File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Lock an object concept que 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 "Lock an object concept que" Watch "Lock an object concept que" New topic

Lock an object concept que

Virendra Singh

Joined: Apr 11, 2007
Posts: 3
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...

SCJP 1.5<br />SCWCD 5
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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


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.


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...

JDBCSupport - An easy to use, light-weight JDBC framework -
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
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.

Experience and talent are independent of age
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
That's a really good analogy.

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
subject: Lock an object concept que
It's not a secret anymore!