Thread Deadlocks - Java Tutorials

Thread Deadlocks - Java Tutorials


 

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

Thread Deadlock Detection in Java

Thread deadlock relates to the multitasking. When two threads have circular dependency on a synchronized, deadlock is possible.

In other words, a situation where a thread is waiting for an object lock that holds by second thread, and this second thread is waiting for an object lock that holds by first thread, this situation is known as Deadlock.

Example

In the given below example, we have created two classes X & Y. They have methods foo() & bar() respectively. they pause for a short period before trying to invoke a method in the other class.

In this example, you will see that while waiting for the monitor on x ,RacingTread have monitor on y. Simultaneously, MainThread have x and is waiting for y. This program will never complete. Means it will have a deadlock

class X {
synchronized void foo(Y y) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered X.foo"); 
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("X Interrupted");
}
System.out.println(name + " trying to call Y.last()");
y.last();
}
synchronized void last() {
System.out.println("Inside X.last");
}
}
class B {
synchronized void bar(X x) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered Y.bar");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("Y Interrupted");
}
System.out.println(name + " trying to call Y.last()");
x.last();
}
synchronized void last() {
System.out.println("Inside X.last");
}
}
class Deadlock implements Runnable {
X x= new X();
Y y= new Y();
Deadlock() {
Thread.currentThread().setName("MainThread");
Thread t = new Thread(this, "RacingThread");
t.start();
x.foo(y); // get lock on a in this thread.
System.out.println("Back in main thread");
}
public void run() {
b.bar(x); // get lock on b in other thread.
System.out.println("Back in other thread");
}
public static void main(String args[]) {
new Deadlock();
}
}

Output

MainThread entered X.foo
RacingThread entered Y.bar
MainThread trying to call Y.last()
RacingThread trying to call X.last()                                                                                           

Deadlock Prevention

Given below the three techniques  to prevent your code from deadlock :

Lock ordering

In this technique all locks are obtained in the same sequence by any thread. This way you can prevent deadlock occurrence.

Lock Timeout

If a thread doesn't succeed in taking all the necessary locks within a certain amount of time period, it will realease all the locks held by it. And again try after a random time.

Deadlock Detection

It is a heavy deadlock prevention technique. It is targeted to design to implement in the condition where Lock ordering and Lock timeout is not worked effectively.

In this technique, we use data structure(For example-map, graph etc) to note down the lock taken by a thread. Also the request by a thread for lock, is also note  down into the data structure.

 When ever a request for the lock is denied, the thread traverse the lock graph to check deadlock.

Advertisements

Ads
Share on Google+Share on Google+

Thread Deadlocks - Java Tutorials

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Automatically Detecting Thread Deadlocks - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads