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 multiple synchronized block in class.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "multiple synchronized block in class.." Watch "multiple synchronized block in class.." New topic
Author

multiple synchronized block in class..

vipin dwivedi
Greenhorn

Joined: Oct 09, 2003
Posts: 4
Hi all,
I have a small doubt any one can clearit ...
if a class have multiple Synch. methods or blocks ,then only one lock dfined for that class or multiple locks are defined for each synch. block.
//dummy code
class a{
synchronized m1(){
m2();
}
synchronized m2(){
}
}
Now for the same class if thread t1 calles m1() ,and t2 calles m2()..Then is it possible to call m2() by t2 untill m1() exits for t1.
practically its working ..... my question is how it is working ,by Scheduling ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I don't understand your last sentence or two about what's happening or what you expect to happen, but I can tell you that there is just one lock per object, and that while one thread is running in m1() or m2(), no other thread will be able to enter either of these methods.


[Jess in Action][AskingGoodQuestions]
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Originally posted by vipin dwivedi:
Hi all,
I have a small doubt any one can clearit ...
if a class have multiple Synch. methods or blocks ,then only one lock dfined for that class or multiple locks are defined for each synch. block.
//dummy code
class a{
synchronized m1(){
m2();
}
synchronized m2(){
}
}
Now for the same class if thread t1 calles m1() ,and t2 calles m2()..Then is it possible to call m2() by t2 untill m1() exits for t1.
practically its working ..... my question is how it is working ,by Scheduling ?

Hi Vipin,
Ur question is a bit vague. Let me see if I can answer ur question. If t1 and t2 try to enter m1 and m2 at the same time, then they will be able to enter both the methods. But thread t1 has to wait until t2 exits m2 since m2 is already being used by t2 and t2 holds the lock.
Hope this helps,
Vinod.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34


If t1 and t2 try to enter m1 and m2 at the same time, then they will be able to enter both the methods. But thread t1 has to wait until t2 exits m2 since m2 is already being used by t2 and t2 holds the lock.

Sorry, but this is wrong. There is only one lock which covers both methods. If thread t2 is already running in m2, then thread t1 will not be able to begin running m1 until m2 returns in t2.
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
I am a little surprised. But have a look at the code and the output. This is contradicts ur explaination.
Thanks,
Vinod.

Originally posted by Ernest Friedman-Hill:

Sorry, but this is wrong. There is only one lock which covers both methods. If thread t2 is already running in m2, then thread t1 will not be able to begin running m1 until m2 returns in t2.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34


But have a look at the code and the output. This is contradicts ur explaination.

No, not at all. In the original example, there were no wait() calls. wait() changes everything! The wait() method can only be called from inside a synchronized method or block, and it explicitly surrenders the lock that the synchronized block acquired. At this point, another Thread that may have been waiting to grab the lock is free to proceed. wait() can't return until that second thread has released the lock. This is all described in the Javadocs for java.lang.Object.
Maybe you meant to use Thread.sleep() instead of wait()? In any case,if the wait() calls were changed to Thread.sleep() calls, you'd find that the output changes dramatically; during a sleep() call, a Thread retains all the locks that it held before the call.
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Got it.. U r the man.
- Vinod.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple synchronized block in class..
 
Similar Threads
synchronized methods
Working of notify()/wait()
Two sync methods of same class called by two threads
Static and non-static synchronized method
Synchronized method behavior