Results 1 to 3 of 3
Thread: Thread Deadlock
- 01-15-2009, 11:48 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
Thread Deadlock
Hi,
The following is a code which generally results in a thread deadlock.
public class Test2 {
public static void main(String[] args) {
OwnThread ot = new OwnThread();
Thread t1 = new Thread(ot);
Thread t2 = new Thread(ot);
t1.start();
t2.start();
}
}
class OwnThread extends Thread {
A a = new A();
B b = new B();
public void run() {
synchronized (a) {
System.out.println(Thread.currentThread().getName( ) + " A");
synchronized (b) {
System.out.println(Thread.currentThread().getName( ) + " B");
}
}
synchronized (b) {
System.out.println(Thread.currentThread().getName( ) + " B");
synchronized (a) {
System.out.println(Thread.currentThread().getName( ) + " A");
}
}
}
}
When there is no deadlock (which is very rare), i will get 8 lines printed - something like:
Thread-1 A
Thread-1 B
Thread-2 A
Thread-2 B
Thread-2 B
Thread-2 A
Thread-1 B
Thread-1 A
On thread deadlocks, i get 4 lines printed - something like:
Thread-1 A
Thread-1 B
Thread-2 A
Thread-1 B
The output is definitely uncertain.
Now, I need to break this deadlock by using wait() and notify(), where do i put these methods so that deadlock never happens ?
- 01-15-2009, 11:39 PM #2
Java Code:class OwnThread extends Thread { A a = new A(); B b = new B(); public void run() { synchronized (a) { System.out.println(getId(" A")); synchronized (b) { b.notify(); System.out.println(getId(" B in A")); } } synchronized (b) { System.out.println(getId(" B")); try { b.wait(); } catch(InterruptedException e) { System.out.println(getId(" interrupt in B")); } synchronized (a) { System.out.println(getId(" A in B")); } b.notify(); } } private String getId(String s) { return Thread.currentThread().getName( ) + s; } }
- 01-16-2009, 02:49 AM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
I think using wait/notify would be an odd solution to this problem, and one liable to have subtle bugs unless you're really careful. (For example, in the example above, one bug is that both threads could run notify() before either starts to wait(), and then the two threads will just sit waiting forever for a notify() that never comes.)
Some typical solutions are:
(a) in cases where you really need to hold two synchronized locks, make sure there is a determined order every time (e.g. use the numerical order of System.identityHashCode() of the objects in question);
(b) use Java 5 Lock classes, or one of the other classes from the Java concurrency framework, which allow you to wait with a timeout, or which allow you to "tentatively" acquire a lock/perform an action on a concurrent structure, and take appropriate action in case of failure. (E.g. don't synchronize forever waiting for things like logging queues: if you don't manage to post an item to the queue in 500ms, then "Something Has Gone Wrong" and there's not point blocking forever.)
If you must use wait/notify, I would recommend encapsulating the construct in your own lock class. (See my example of a pre-Java 5 lock implementation with wait/notify based on this idea.) But I really only recommend this if, say, you're running in a version pre Java 5.Last edited by neilcoffey; 01-16-2009 at 02:52 AM. Reason: Added extra info
Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
How to create a simple deadlock
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:36 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
Deadlock detection tools documentation
By goldhouse in forum Threads and SynchronizationReplies: 0Last Post: 07-18-2007, 05:18 AM


LinkBack URL
About LinkBacks

Bookmarks