Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Threads?

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic