Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Synchronization??

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very Good Question, I am also working on the same question....

Not resolved till now...

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone here to explain the above code??
 
Parambir Singh
Ranch Hand
Posts: 40
Android Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
David Grindley
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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??
 
wang gang
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic