File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring 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: 4541

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:
subject: Synchronize keyword
It's not a secret anymore!