• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

deadlock?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I somehow managed to solve the prev problem.
It was about the share on the 2 objects I wanted to deadlock...
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic