wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes deadlock? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "deadlock?" Watch "deadlock?" New topic
Author

deadlock?

Vali GB
Greenhorn

Joined: Jun 06, 2004
Posts: 7
Hi all,

try to get a deadlock accessing via 2 threads 2 resources in synchronized blocks like:

synchronized(A){
synchronized(B){
}
}
and
synchronized(B){
synchronized(A){
}
}
i try to put the threads on sleep, use system.outs just to make sure my threads are accessing these objects in a convenient way, i know this because i got this output(copied/pasted from one run):
1. u tryin to acquire lock on A
2. u got lock on A, tryin to get lock on B
3. v tryin to acquire lock on B
4. v got lock on B, tryin to get lock on A
5. v got lock on A
6. v thread, freed all locks
7. u got lock on B
... and so on..

so, my question, how's it possible to read the 5th line if the u thread has already the lock on A?

Thanks,
vali
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Threads and Synchronization forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Siyaa Hoffman
Greenhorn

Joined: Jan 15, 2004
Posts: 23
Hi Vali,

Try this code. This creates a deadlock between the two threads.

public class TestThreadSync implements Runnable
{
String A = new String("Siyaa1");
String B = new String("Siyaa2");

public static void main(String[] args)
{
TestThreadSync mainClass = new TestThreadSync();
Thread t1 = new Thread(mainClass);
Thread t2 = new Thread(mainClass);
t1.setName("T1");
t2.setName("T2");
t1.start();
t2.start();
}


public void run()
{
try{
if(Thread.currentThread().getName().equals("T1"))
{
System.out.println("Thread T1 launched");
synchronized(A)
{
System.out.println("Thread T1 got lock on A");
Thread.sleep(10000);

synchronized(B){
System.out.println("T1 managed both");
}
}
}

if(Thread.currentThread().getName().equals("T2"))
{
System.out.println("Thread T2 launched");
synchronized(B)
{
System.out.println("Thread T2 got lock on B");
Thread.sleep(10000);

synchronized(A){
System.out.println("T2 managed both");
}
}
}

}
catch(Exception e)
{
System.out.println("Exception - "+e.getMessage());
}
}




}

The output that I see is -

C:\Test>java TestThreadSync
Thread T1 launched
Thread T1 got lock on A
Thread T2 launched
Thread T2 got lock on B


And the program just hangs there. This is what I expected it to do when entered in a deadlock situation.
Vali GB
Greenhorn

Joined: Jun 06, 2004
Posts: 7
Thanx a lot Siyaa!!
your example worked just fine into a deadlock..of course

actually my test program ressembled a lot yours with a few differences, and I still can't understand something...

For example, if I modify your program just a bit :

the're will be no more deadlock:

Thread T1 launched
Thread T1 got lock on A
Thread T2 launched
Thread T2 got lock on B
T1 managed both
T2 managed both
Process terminated with exit code 0

Why?
Vali GB
Greenhorn

Joined: Jun 06, 2004
Posts: 7
I somehow managed to solve the prev problem.
It was about the share on the 2 objects I wanted to deadlock...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: deadlock?
 
Similar Threads
Data Locking with a DB File.
deadlock?
Thread synchronize question from JQ+ test
Threads Notes for Exams
Please suggest ways to cause deadlock in this code