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