aspose file tools*
The moose likes Threads and Synchronization and the fly likes Returning from synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Returning from synchronized" Watch "Returning from synchronized" New topic
Author

Returning from synchronized

Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Let's say I've the following code segments:

In case of [a] what will happen if some thread currently holding the object of the method update1() and returning some value from that method. Does it release the lock upon executing return?
In case of [b] assume that one thread (say A) held the lock of the object of the method update2(). After executing someMethod() does the thread A hold the same lock or release it? Assuming that someMethod() is also synchronized and is in the same class, What happens then thread A still be able to execute the body of someMethod()?
Finally is it safe to return from a synchronized block or method?
Thanks
Prasad
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
In case of [a] what will happen if some thread currently holding the object of the method update1() and returning some value from that method. Does it release the lock upon executing return?
If thread T1 is executing the method update1() of the object O1, and you invoke method update1() or update2() of the object O1 from thread T2, then thread B will wait until thread T1 executed the synchronized method. Notice that I explicitely mentioned 3 entities here: object O1, thread T1, and thread T2. The situation could be different: if thread T1 is executing the method update1() of the object O1, and you invoke method update1() or update2() of the object O2 from thread T2, then thread T1 does not need to wait for thread T2, and vise versa. In other words, the thread contention comes into the question only when 2 or more threads compete for the same resource/object.

In case of [b] assume that one thread (say A) held the lock of the object of the method update2(). After executing someMethod() does the thread A hold the same lock or release it? Assuming that someMethod() is also synchronized and is in the same class, What happens then thread A still be able to execute the body of someMethod()?
Yes, thread A will be able to continue execution, because it will already have the monitor on the object. In your scenario, the object in question is this. Note that the code:

is exactly equavalent to

In effect, this means that your method SomeMethod() doesn't need to be synchronized, as long as it is invoked from a synchronized method and doesn't access any other shared resources.
Now, you have yet another level of synchronization around object privateObj, and that's also not neccessary, as long as privateObj is some object private to the class where you are manipulating it from the synchronized methods. Finally, this multi-level synchronization is a very dangerous thing and is a classic recipie to deadlocks. For example, if sometime in the future you add some code to your class where you synchronize on privateObj and then synchronize on this, you are almost guaranteed a problem, -- at some point in time, one thread will lock on privateObj and wait until this becomes unlocked, which may never happen because the other thread already locked on this and was waiting for privateObj to become unlocked.
Finally is it safe to return from a synchronized block or method?
Yes, as safe/unsafe as to do anything else that I can think of. Not sure why you question the return specifically.
[ November 30, 2003: Message edited by: Eugene Kononov ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning from synchronized