• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized problem

 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether deadlock is possible in above scenarion? If not then which modification will always avoid the deadlock scenario.
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic