• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Synchronization??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

The code below is from Dan Chisholm,


Can anyone please explain me the flow?? I would be really greatful.

Thanks in advance.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very Good Question, I am also working on the same question....

Not resolved till now...

 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone here to explain the above code??
 
Ranch Hand
Posts: 40
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the answer given in the book. Either the program will print ABC or it will continue to wait forever. The behavior of program cannot be predicted as we can't be sure which thread (B or A) will get hold of object's lock first.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result is always ABC.

There are two possible scenarios :

1. B.main enters the synchronised section first and updates the values in the array sa. Even if the method A.run() starts to execute it has to stop outside the synchronised block until B.main() releases the lock it has on the sa array. When B.main() leaves the synchronised block then A.run() can enter it. Since sa[0] is now "Done" the while loop gets skipped and the remaining values in sa get printed out.

2. A.run() enters the synchronised section first and checks the value of sa[0]. Since this is still "Not Done" the method enters a wait and releases the lock it has on the sa object. B.main can now enter the synchronised section and update the values in sa. The call to sa.notify() marks the thread A as runnable so once B.main leaves the synchronised block then A.run() can regain the lock and check the value of sa[0]. Since sa[0] is now "Done" the while loop finishes and the remaining values in sa get printed out.

Hopefully the above is clear enough.

All the best

Dave
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation. But my doubt here is, String[] sa; is declared in both the classes. So this makes me confuse if both the threads will wait for the other sa or will it use it's own sa??
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reply:Jothi Shankar Kumar Sankararaj
i think you thinked too enough. you may forget the true means of reference variable. because "private static String[] sa = new String[]{"Not Done","X","Y","Z"};" then sa is passed to A's constructor. so the two "sa " refer the same object. that's my opinion.
 
David Grindley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi

True, both classes have a reference sa[]. However a string array object is only created in class B. a reference to this object is passed to class A when thread t1 is created in B.main(). At this point both B.sa and t1.sa refer to the same object, the one and only String array object that is created and referenced in this example. Hence the synchronized statement in both A and B will try and lock on the same object.

All the best

Dave
 
Whatever. Here's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic