Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning from synchronized

 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic