File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Who is getting locked? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Who is getting locked?" Watch "Who is getting locked?" New topic
Author

Who is getting locked?

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 137

Hi all.

I have a conceptual doubt about threads. If I have an object that has the method that follows:



I'd like to confirm that the object whose method is synchronized is the object that has been locked.
So the instance variables such as acct who is a reference to an object isn't locked.

Am I right?

Thanks


Adolfo Eloy
Software Developer
OCPJP 6
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3133
We don't really refer to anything as "being locked." Rather, we say that we obtain a lock. But if by "locking X" you mean "obtaining X's lock", then, yes, you're correct. The object whose lock we obtain is the one whose synchronized method we are executing.



The above methods are equivalent. Both obtain the lock for the "this" object and hold it for the entire body of the method.
 
 
subject: Who is getting locked?
 
Threads others viewed
Synchronized method won't let other thread go, why?
join() vs. synchronized
Confused with Synchronization
Synchronised Vs. Non synchronised Thread
Problem with threads (concurrency)
MyEclipse, The Clear Choice