• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

multiple synchronized block in class..

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.. U r the man.
- Vinod.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic