Tutorial Addendum On Java - Deadlocks

 31 December 18:00   

    



    



    

What Is Deadlock

    



    

Deadlock: A accompaniment of beheading if 2 or added accoutrement are all put on hold,

    

because anniversary of them is captivation a synchronization lock while cat-and-mouse for another

    

lock. The lock anniversary cilia is cat-and-mouse for is captivated by one of the additional threads.

    

So none of accoutrement can move forward.

    



    

By definition, deadlock can alone appear if the program is active assorted threads,

    

and assorted locks are getting acclimated by assorted threads. Therefore:

    



        

  • A single-threaded program will never accept deadlocks.

        



  •     

  • A program with one lock will never accept deadlocks.

        



  •     



    



    

Here is a simple program to authenticate a deadlock with two accoutrement and two

    

locks:

    

 

    

/**

    

* SimpleDeadLock.java

    

* Absorb (c) 2002 by Dr. Yang

    

*/

    

import java.util.*;

    

public chic SimpleDeadLock extends Cilia {

    

accessible changeless Item l1 = new Object();

    

accessible changeless Item l2 = new Object();

    

clandestine int index;

    

accessible changeless abandoned main(String[] a) {

    

Cilia t1 = new Thread1();

    

Cilia t2 = new Thread2();

    

t1.start();

    

t2.start();

    

}

    

clandestine changeless chic Thread1 extends Cilia {

    

accessible abandoned run() {

    

synchronized (l1) {

    

System.out.println("Thread 1: Captivation lock 1...");

    

try { Thread.sleep(10); }

    

bolt (InterruptedException e) {}

    

System.out.println("Thread 1: Cat-and-mouse for lock 2...");

    

synchronized (l2) {

    

System.out.println("Thread 2: Captivation lock 1 & 2...");

    

}

    

}

    

}

    

}

    

clandestine changeless chic Thread2 extends Cilia {

    

accessible abandoned run() {

    

synchronized (l2) {

    

System.out.println("Thread 2: Captivation lock 2...");

    

try { Thread.sleep(10); }

    

bolt (InterruptedException e) {}

    

System.out.println("Thread 2: Cat-and-mouse for lock 1...");

    

synchronized (l1) {

    

System.out.println("Thread 2: Captivation lock 2 & 1...");

    

}

    

}

    

}

    

}

    

}

    



    



    

Output:

    

 

    

Thread 1: Captivation lock 1...

    

Thread 2: Captivation lock 2...

    

Thread 1: Cat-and-mouse for lock 2...

    

Thread 2: Cat-and-mouse for lock 1...

    



    



    

As the achievement shows, afterwards accepting lock 1, cilia 1 was put on authority to

    

wait for lock 2, which was captivated by cilia 2 and it will never be release,

    

because cilia 2 was aswell put on authority to delay lock 1, which was captivated by

    

thread1. So none of them could move forward. You accept to columnist Ctrl-C

    

to stop program.

    



    

5 Dining Philosophers

    

The adventure of 5 dining philosophers is the alotof absorbing analogy of the

    

deadlock problem. Actuality is the adaptation of the adventure from the "The Java Tutorial" by

    

Sun Microsystems.

    

"

    

Five philosophers are sitting at a annular table. In foreground of anniversary philosopher is

    

a basin of rice. Amid anniversary brace of philosophers is one chopstick. Afore an

    

individual philosopher can yield a chaw of rice, he haveto accept two chopsticks,

    

one taken from the left, and one taken from the right.

    

The philosophers haveto acquisition some way to allotment chopsticks such that they all get to eat.

    

"

    



    

Deadlock will action in this story, if all philosophers are demography the chopstick

    

from the larboard ancillary at the aforementioned time, and no one is accommodating to put down the chopstick

    

until he gets the chopstick on the appropriate side.

    



    

 


Tags: program, story, system, class, public, tutorial, object

 thread, holding, println, threads, system, public, static, program, philosophers, deadlocks, object, synchronized, deadlock, story, class, multiple, tutorial, extends, private, thread1, , holding lock, println thread, system out, public static, extends thread, one taken from, private static class, public static object,

Share Tutorial Addendum On Java - Deadlocks:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  java