This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes java synchronize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "java synchronize method" Watch "java synchronize method" New topic
Author

java synchronize method

Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
As I know, if I add a synchronize modifier to a method, then the method can only access by single thread at any time, then what if the thread calling wait() method inside the synchronize method?? Will other thread then allow to enter that method?? Sometime like the following, I see that both the "Before" statement will print first, then is it mean that the synchronize modifier has no guarantee that only one thread can enter it in any time, or there are another condition to it and what actually does the synchronize modifier ensure in concurrent programming?? In addition, if I want to ensure that after no other thread can enter the test() method before one of the thread has totally complete the whole test() method (printing before and after statement), then what I can do??
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Arthur Sc Chan wrote:then what if the thread calling wait() method inside the synchronize method?? Will other thread then allow to enter that method??

Yes. Because, wait method releases the lock. So, the moment you invoke wait from T1, it releases the lock and T2 enters in that method. This is the reason why you are getting both 'Before' statements first.
Arthur Sc Chan wrote:is it mean that the synchronize modifier has no guarantee that only one thread can enter it in any time

No. Its not like that. Here, the problem is - code is releasing the lock and hence another thread is entering in that method.
Arthur Sc Chan wrote:In addition, if I want to ensure that after no other thread can enter the test() method before one of the thread has totally complete the whole test() method (printing before and after statement), then what I can do??

Either remove call to wait from test method, or replace it with Thread.sleep(1000) (if you want it).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Arthur Sc Chan
Greenhorn

Joined: Sep 07, 2010
Posts: 11
Thanks for the reply.
In addition, now there are two thread in the synchronize block, what will happen if one of the thread reach the timeout of the wait() method (1000 ms) while other thread is still running in the synchronize method (with the lock)?? is it the case that they will compete to regain the lock?? Or the just timeout method will wait until that thread release the lock?? Also, if it it the case that, a thread (t1) pass the wait method on the same time that another new thread (t3) enter the synchronize method, then will t1 have a higher priority to regain the lock or it is just a random assign of the lock to t1 and t3??
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Arthur Sc Chan wrote:is it the case that they will compete to regain the lock??

No. As you said, one thread is already having the lock, so there's no competition here
Another thread (whose wait method has timed out) will now keep on trying to acquire the lock and will wait for that split second moment when the thread with the lock will release the lock (either due to wait method, or due to completion of execution of synchronized block).
Arthur Sc Chan wrote:will t1 have a higher priority to regain the lock or it is just a random assign of the lock to t1 and t3??

That depends on the algorithm. Frankly, I don't know the answer for this, but you should never code your application by keeping a particular algorithm in mind - i.e. the behavior should not depend on scheduling algorithm. When you say that the code is thread safe, it must be thread safe no matter what kind of scheduling algorithm is used by JVM.

I hope this helps.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Arthur Sc Chan wrote:As I know, if I add a synchronize modifier to a method, then the method can only access by single thread at any time,


If it's a non-static ethod, then only one thread at a time can access that method on that object, but if you have multiple objects, then you have multiple different locks, so T1 might be executing the method on Obj1 while T2 is executing the method on Obj2.
 
 
subject: java synchronize method