aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Synchronization?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Synchronization??" Watch "Thread Synchronization??" New topic
Author

Thread Synchronization??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9385
    
    2

Guys,

The code below is from Dan Chisholm,


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

Thanks in advance.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Very Good Question, I am also working on the same question....

Not resolved till now...



The Best way to predict your future is to create it - Every great individual common man
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9385
    
    2

Anyone here to explain the above code??
Parambir Singh
Ranch Hand

Joined: Sep 05, 2004
Posts: 40

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.


SCJP, SCMAD
David Grindley
Greenhorn

Joined: Oct 30, 2006
Posts: 14
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

Joined: Sep 26, 2006
Posts: 9385
    
    2

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

Joined: Dec 08, 2006
Posts: 2
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.


faquir
David Grindley
Greenhorn

Joined: Oct 30, 2006
Posts: 14
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Synchronization??