Results 1 to 4 of 4
- 04-16-2010, 12:05 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
How to avoid the Deadlock in the below program
Hello Friends,
I am learning java now.I know the following program has deadlock, now i trying the avoid the deadlock for this
program so please post your some suggestions how to avoid the deadlock for the below.
class A
{
synchronized void foo(B b)
{
System.out.println(Thread.currentThread().getName( )+" Entered into A.foo()");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("A Caught Exception");
}
System.out.println(Thread.currentThread().getName( )+" Now trying to Access b.last()");
b.last();
System.out.println(Thread.currentThread().getName( )+" Now trying to Access b.Info()");
b.info();
}
synchronized void last()
{
System.out.println(Thread.currentThread().getName( )+" Entered into A.last()");
}
void msg()
{
System.out.println(Thread.currentThread().getName( )+" Entered into A.msg()");
}
}
class B
{
synchronized void bar(A a)
{
System.out.println(Thread.currentThread().getName( )+" Entered into B.bar()");
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println("B Caught Exception");
}
System.out.println(Thread.currentThread().getName( )+" Now trying to Access A.last()");
a.last();
System.out.println(Thread.currentThread().getName( )+" Now trying to Access A.msg()");
a.msg();
}
synchronized void last()
{
System.out.println(Thread.currentThread().getName( )+" Entered into B.last()");
}
void info()
{
System.out.println(Thread.currentThread().getName( )+" Entered into B.info()");
}
}
class DeadLockTest2 implements Runnable
{
A a=new A();
B b=new B();
DeadLockTest2()
{
Thread.currentThread().setName("Thread1");
System.out.println(Thread.currentThread().getName( )+" Is Now Running");
new Thread(this,"Thread2").start();
System.out.println(Thread.currentThread().getName( )+" Is Now Running");
System.out.println(Thread.currentThread().getName( )+" is trying to get the lock on A.foo()");
a.foo(b);
System.out.println(Thread.currentThread().getName( )+" is got the lock on A.foo()");
nonSyn();
}
public void run()
{
System.out.println(Thread.currentThread().getName( )+" Is Now Running");
System.out.println(Thread.currentThread().getName( )+" is trying to get the lock on B.bar()");
b.bar(a);
System.out.println(Thread.currentThread().getName( )+" is got the lock on B.bar()");
nonSyn();
}
public void nonSyn()
{
a.msg();
b.info();
}
public static void main(String[] args)
{
new DeadLockTest2();
}
}
- 04-16-2010, 12:14 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
when you have 2 locks, you must always access them in the same order:
works fine when you call it from different threads, whereasJava Code:void method1 () { synchronized (lock1) { synchronized (lock2) { // do stuff } } } void method2 () { synchronized (lock1) { synchronized (lock2) { // do other stuff } } }
will result in a dead lock. You're using the second approach.Java Code:void method1 () { synchronized (lock1) { synchronized (lock2) { // do stuff } } } void method2 () { synchronized (lock2) { synchronized (lock1) { // do other stuff } } }
- 04-16-2010, 05:36 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Token of Gratitude
Hi Iluxa,
Thank you so much for your valuable time and I understood the way you avoid the deadlock.I would like to know Is this the only of way(ordering of locks) to avoid the deadlock or do we have any other approach to avoid deadlocks.Please let me know.
- 04-16-2010, 05:46 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
the ordering of locks is pretty much the only way (at least that I'm aware of) to GUARANTEE things will not go wrong. If you implement another mechanism and you're extremely careful and think of all possible race conditions, you could be OK as well, but this approach is a good simple safety net.
side note: it's considered bad practice to do things like
because it's not really clear what you're locking on. previous snippet of code is equivalent toJava Code:public synchronized void doStuff () { //... }
it's not considered good practice to synchronize on "this". Instead, create a meaningful object and use that:Java Code:public void doStuff () { synchronized (this) { //... } }
Java Code:class MyClass { private final Object readLock = new Object (); private final Object writeLock = new Object (); public void readStuff () { synchronized (readLock) { //... } } public void writeStuff () { synchronized (writeLock) { //... } } }
Similar Threads
-
[SOLVED] Deadlock ?
By jon80 in forum New To JavaReplies: 1Last Post: 06-14-2009, 07:54 PM -
Analysing for starvation, deadlock and mutual exclusion
By scarymovie in forum New To JavaReplies: 0Last Post: 03-07-2009, 06:58 AM -
Thread Deadlock
By ajeeb in forum New To JavaReplies: 2Last Post: 01-16-2009, 02:49 AM -
How to create a simple deadlock
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:36 PM -
Deadlock detection tools documentation
By goldhouse in forum Threads and SynchronizationReplies: 0Last Post: 07-18-2007, 05:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks