This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Doubt on threading

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers!.I was able to understand the deadlock situation down under but was not able to understand how the second answer is B.Could some one explain please?The second answer is B. The program prints �CDDACB�

code:
---------------------------------------------------------------------------
1. public class SyncTest{
2. public static void main(String[] args) {
3. final StringBuffer s1= new StringBuffer();
4. final StringBuffer s2= new StringBuffer();
5. new Thread () {
6. public void run() {
7. synchronized(s1) {
8. s2.append(�A�);
9. synchronized(s2) {
10. s2.append(�B�);
11. System.out.print(s1);
12. System.out.print(s2);
13. }
14. }
15. }
16. }.start();
17. new Thread() {
18. public void run() {
19. synchronized(s2) {
20. s2.append(�C�);
21. synchronized(s1) {
22. s1.append(�D�);
23. System.out.print(s2);
24. System.out.print(s1);
25. }
26. }
27. }
28. }.start();
29. }
30. }
--------------------------------------------------------------------------

Thanks in Advance
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources.

The code doesn't deadlock for me. It prints "ABABCD".
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the source is one of the Exam simulators.Also there is a chance of deadlock in this particular situation according to KB(310-035 Pg 526).What I dont understand is the program flow.Could you please tell me how you got to the answer?.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well the source is one of the Exam simulators.


Please be specific.

Could you please tell me how you got to the answer?


That was easy:


A deadlock can happen because both threads need the same locks, but don't acquire them in the same order. If the first thread gets to line 8, and then the second thread gets to line 20, both have one lock each, but can't get the other one, because it's help by the other thread --> deadlock.
[ July 29, 2007: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

Rahul wrote:

The second answer is B. The program prints “CDDACB”



Here's a copy of the code with indentations:



For me the program freezes. (Windoze, Java 1.5.0) Did you change something to produce the output CDDACB?

Also with Eclipse 3.1 it freezes.



And:

I was able to understand the deadlock situation down under (...)




Perhaps somebody should inform the government in Canberra about this.



sorry, couldn't resist

Welcome to the Ranch, Rahul!





Yours,
Bu.
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf:

What I didnot understand is how you got ABABCD.Could you please explain the program flow?.
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf,I was not able to quote the source since this site filters it.I appreciate your explanation with the deadlock concept.Could someone explain me the program flow?.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rahul siddharth:
Hi Ulf,I was not able to quote the source since this site filters it.


Please send me a private message with the text of the message you're getting when you try to post it, and also include the exact source of the question. Thanks.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I ran the code with eclipse 3.1 and on command prompt and the outputis
ABABCD. However if i have breakpoints in the run() of each annonoymous inner classes , I can see the deadlock.

I think when we run the code without any breakpoint the first thread has
already reached the second synchronized block and hence now it has both s1 ad s2 locks whth itself and second thread cannot enter its first synchronized block because the locks s1 and s2 are held by first thread.
Now first thread finshes it execution and the result will be
s2 = AB and s1 is still empty hence the output is
AB
Now since first thread has completed execution it releases both the locks and second thread now can continue with its execution it appends CD to s1 and prints ABCD
hence the complete result is
ABABCD

However if you have brekpoints in both the run() you can clearly see that
first thread will acquire a lock on s2 and now debug the second thread which will acquire the lock on s1 and now there is a deadlock beacuse each of the thread cannot enter their next synchronized blocks beacause the locks are held by each other.

There is every possiblity that the above deadlock can happen with normal execution as well. So this code can cause deadlock.
Thanks
Deepak
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Deepak

Dammm gud explanation.....







Preparing SCJP 1.5
 
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