aspose file tools*
The moose likes Threads and Synchronization and the fly likes confusion about object lock. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "confusion about object lock." Watch "confusion about object lock." New topic
Author

confusion about object lock.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

Hi All,

I have a class as shown below:



this class has two methods they are synchronized. Now let us say thread 1 is executing firstMethod while its executing that thread 2 tries to execute the second method on same object. Will it be able to execute it or not? If yes then how come this is lock on a object? I hopw this is not a silly question. Please help.


Pawan Chopra
SCJP - DuMmIeS mInD
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

When a synchronized method of an object is called, all other synchronized of the same object will block until the executed method ends.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Here is an example where the first method sleeps 5 seconds. Check what happens with and without the synchronized keyword on the second method.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

Thanks a lot Christophe!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion about object lock.