Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lock an object concept que

 
Virendra Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is based on synchronize method or block. As I have concept that when you synchronize any method, It acquire the lock associated with the current instance of the class whose code we're executing (means "this" object). what is that exactly means:
>> means No other thread can access "this" object until lock is established. (OK!!) means no other thread can access "this" object or its method (let it be synchronize or non-synchronize). right!! but I found in Head first book (9th chapter page 707)

once a thread acquires the lock on an object, no other thread can enter any of the synchronized methods in that class (for that object). Means can other thread access non-synchronize method?? If yes, then what that lock means...

I am not able to make clear picture on meaning of lock to "this" object or other object. What happens when lock is acquired on any object? Can you please help me on this. If you can help me with example ..that would be really appreciated!!!


If I am not cleared, Please let me know...
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you have the following construct.



Two threads cannot access doA() at the same time, however, Thread 1 can access doA(), and Thread 2 can access doB() at the same time

Now,



Any call that involves the currently running instance is synchronized ... so Thread 1 and Thread 2 cannot run in either of the two methods at the same time!

synchronized(this) is the same as if you build an instance of Test from somewhere else and then synchronize on it.

Imagine



any invocation or piece of code that involves t from somewhere else other than the synchronized block is blocked until the synchronized block has finished doing that it is doing.

So, if your code is in the synchronized block, and another Thread invokes t.doB() on the SAME instance, it will have to wait until the synchronized block has finished...
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there is a restaurant with 2 types of halls, a banquet and a diners' hall. You want to organize a party and book the banquet for yourself. While you have the party there, nobody else can organize his/her own party in the same banquet but yes the diners' hall is open for general public.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a really good analogy.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes following on himanshu's analogy.....the more simple word for synchronized here is "reserved" . Once you book the hall(Acquire a lock) it is reserved for that person who booked until his function is over. Once his function is over, the reservation is again open for whoever wants to reserve that hall(Release the lock). If you are so worried about the reservation some people book the entire Hotel hehe (synchronized(hotel.class)) So no one will use anything of that class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic