wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized Objects" Watch "Synchronized Objects" New topic
Author

Synchronized Objects

Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
Can somebody help me "visualize" the following code (from Dan Chisholm's site)?

PS
Corey McGlone's illustration of parameter passing in Java has been very helpful. Hope somebody can explain the above code similarly.
[ January 27, 2003: Message edited by: Maria Garcia ]

SCJP 1.4
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
Hi Maria,
Here's what I think is happening:
After line 6 executes we have two threads in a runnable state: the main one (thread main) and the one executing the run method of class A (thread 0).
By means of lines 1 and 7 they both fight for the lock on String array sa which at this point (after line 6) is
a[0]->"Not Done", a[1]->"X", a[2]->"Y", a[3]->"Z"
.
Scenario 1
Thread 0 gets the lock first, line 1 executes before line 7.
Compares sa[0] with "Done" which results in false.
Enters the while loop and executes the wait method of the sa object.
The wait method makes the calling thread unlock all it's locks (including the lock on sa) and puts the thread to "sleep" but unlike the sleep method doesn't keep the locks.
While thread 0 sleeps thread main who in the main time was blocked waiting at line 7 suddenly finds the sa String array unlocked, locks it and executes lines 8,9,10,11,12 and exits the block.
Now sa is:
a[0]->"Done", a[1]->"A", a[2]->"B", a[3]->"C"
Line 12 sends a notification to one of the threads (in this case only one, thread 0) waiting for the sa object and wakes up a thread (here thread 0).
Thread 0 wakes up, executes line 2, finds the expression false and exits the loop and executes line 4 printing ABC.
Scenario 2
Thread main gets the lock first, line 7 executes before line 1.
Thread main locks sa, executes lines 8,9,10,11,12 and exits the block.
Thread 0 finds the while expression on line 2 false and doesn't even bother to enter the while loop but goes directly to line 4 and prints ABC.
Hope this was helpful and I hope I didn't oversee anything.

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Originally posted by Maria Garcia:
PS
Corey McGlone's illustration of parameter passing in Java has been very helpful. [ January 27, 2003: Message edited by: Maria Garcia ]


Maria, this may be a little O.T., but i am interested in having a look at Cory McGlone's illustration - is there a URL?
thanks


giddee up
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Dan, your explanation is very good. I don't think I can add anything to that.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
Thank you Dan!!
Here is the link to Corey McGlone's page.
[ January 28, 2003: Message edited by: Maria Garcia ]
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Corey McGlone's page clearly explaine the parameter passing and also there is some light on GC. The way the objects get isolated on heap are shown clearly.

Thanks for sharing such useful info.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Just in case :
wait will release only the monitor associated with the object on which wait was called on. If the thread calling wait holds more locks on other objects, they are still owned by that thread after its call to wait.
In the example the second thread is not allowed to proceed untill main ends the block synchronized on lock1; regardless main thread (t) waits on lock2.
Note: well t is not used in this example, but it was in my previous one.

[ January 29, 2003: Message edited by: Jose Botella ]
[ January 29, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Originally posted by Maria Garcia:
Here is the link to Corey McGlone's page.

thanks for the link Maria!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized Objects
 
Similar Threads
Dan's: Thread Q 39
Thread - Wait Doubt
Threads again
Thread
Thread Synchronization??