It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Threads? 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 "Question on Threads?" Watch "Question on Threads?" New topic

Question on Threads?

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115

In the following program,

The synchronized lock on line 1, does not let threads Lucy and Sam into the blocked code at the same time?
ie)if Lucy enters the synchronized block on line 1 and acquires the lock of "sync" , then Sam cannot enter the synchronized block on line 1 untill Lucy releases the lock?

Is that right?

SCJP 6.0 90%
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Yes, either Lucy or Sam can enter the synchronized block but not at the same time.

It is not guaranteed who will be the first to enter.

Innar Made

Joined: May 12, 2009
Posts: 17
Hey Anuradha,

Yes, you are correct.

No other thread can't get into the synchronized block while some thread owns the sync object's lock. When the synchronized block finishes then sync object lock is released and other threads can start to compete to get it. Whoever gets it can then enter the synchronized block.
I agree. Here's the link:
subject: Question on Threads?
It's not a secret anymore!