learn java
The moose likes Threads and Synchronization and the fly likes Synchronize keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronize keyword" Watch "Synchronize keyword" New topic

Synchronize keyword

Joel Bijapurkar

Joined: Aug 01, 2011
Posts: 20

Suppose i synchronize a instance method in a class and create two objects A and B of the same class. Now when object A is accessing the synchronized method will B also be able to access it or will it have to wait until A releases the lock?
Matthew Brown

Joined: Apr 06, 2010
Posts: 4519

They lock different objects (the instance in question), so the methods will be able to run at the same time.

If you synchronized a static method, that locks the class object, and so then any call can block any other.
Joel Bijapurkar

Joined: Aug 01, 2011
Posts: 20

Thanks Matthew.
I agree. Here's the link: http://aspose.com/file-tools
subject: Synchronize keyword