wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized problem 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 "synchronized problem" Watch "synchronized problem" New topic
Author

synchronized problem

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78

its written in k&b that the this object gets locked..in this case which is getting locked?
if t starts off first then is it going to lock the tl object?what about ty??will it be able to execute?or will it be blocked??
sorry not very clear about the concept..please help me understand this..
kunal dabir
Greenhorn

Joined: Sep 08, 2008
Posts: 15
When thread t's synchronized method starts, tl will be locked. to will wait for tl's lock which will be free only when t's synchronized method is finished.

Also notice, the loop is not in synchronized block or method. Both threads (t & to) will start and the same object tl will be used by both inside the loop. Order of processing is NOT guaranteed. But only one thread will be incrementing (executing increment()) at a given time on object tl.

thread ty is not affected by this as its acting on altogether different object.
Rizwan Patel
Ranch Hand

Joined: Jan 03, 2008
Posts: 59
Whether deadlock is possible in above scenarion? If not then which modification will always avoid the deadlock scenario.


scjp 1.5, OCPJWSD 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Originally posted by Rizwan Patel:
Whether deadlock is possible in above scenarion? If not then which modification will always avoid the deadlock scenario.


Deadlock is not possible in this example.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized problem
 
Similar Threads
Threads again
Beginner Question on threads
Synchornized inside run
Thread holding a lock more than once
Need Explanation of unpredicted output even if methods are synchronized