Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Threads and Deadlock - a question in SCJP exam

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the foll code: it deadlocks because of sleep command in first thread -
my question is
1. How can you have lock over sb2 and append sb1 and then have lock over sb1 and append sb2 ???
2. can you see the deadlock without the sleep command
3. with //1 - it deadlocks
4. with 2 and or 3 it prints only sb1 = X
pls explain - main question is part 1.
public class TestClassDeadlock
{
static StringBuffer sb1 = new StringBuffer();
static StringBuffer sb2 = new StringBuffer();
public static void main(String[] args)
{
new Thread(new Runnable() {public void run()
{
synchronized(sb2) {sb1.append("X");
System.out.println("thread 1 appending X to sb1");
//try{Thread.sleep(5000);} catch(InterruptedException e){} //1
synchronized(sb1) {sb2.append("Y");
System.out.println("thread 1 appending Y to sb2");
}
}
//try{Thread.sleep(500);} catch(InterruptedException e){} // 2
System.out.println("This is sb1 :"+sb1);
}
}
).start();
new Thread(new Runnable(){public void run(){
synchronized(sb1){sb2.append("X");
System.out.println("thread 2 appending X to sb2");
synchronized(sb2){//try{Thread.sleep(500);} catch(InterruptedException e){}//3
sb1.append("Y");
System.out.println("thread 2 appending Y to sb1");
}
}
System.out.println("This is sb2 :"+sb2);
}
}
).start();
}
}
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1)I see no reason why not?As far as I know,when a thread obtains a lock on an object(sb2, in this case)(and suppose it sleeps) and if another thread gains control of the C.P.U then that thread cannot manipulate sb2,unless the lock on sb2 has been released by the former thread that has the monitor still.So here, sb1 is accessible & hence I suppose you can pretty well perform operations on it.
3)The reason why //1 causes a dead lock is-
Thread1 has obtained a lock on sb2.Now,after the statement,
***System.out.println("thread 1 appending X to sb1");***
thread 1 goes to sleep relinquishing the C.P.U but having the lock on sb2 still.The next thread gets a chance to execute & it locks sb1.It then tries to acquire lock on sb2,but in vain.So when thread 2's time is up(in a time sliced environment),thread 2 gives up the CPU(but having the lock on sb1).Now thread one gains control of C.P.U. and eventually tries to lock sb1!!!,resulting in a dead lock state.
2)Yes
4) with 2 and or 3----It printed

thread 1 appending X to sb1
thread 1 appending Y to sb2
thread 2 appending X to sb2
thread 2 appending Y to sb1
This is sb2 :YX
This is sb1 :XY
on some runs and
thread 1 appending X to sb1
thread 1 appending Y to sb2
This is sb1 :X
thread 2 appending X to sb2
thread 2 appending Y to sb1
This is sb2 :YX
on some others.
As someone concluded previously,I believe it's "Platform Dependent"
Do correct me if I'm wrong!
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
priya, my Question no. 1 was a little different
Check the code and you'll see that the first thread enters synchronized sb2 but does sb1.append(...something)
How is this possible ??
it means that saying synchronized sb1 or sb2 doesn't matter.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic