• 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

Synchronized Objects

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, your explanation is very good. I don't think I can add anything to that.
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dan!!
Here is the link to Corey McGlone's page.
[ January 28, 2003: Message edited by: Maria Garcia ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maria Garcia:
Here is the link to Corey McGlone's page.


thanks for the link Maria!
 
reply
    Bookmark Topic Watch Topic
  • New Topic