• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized method behavior

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, I got question about the synchronized method.
class A {
synchronized void m1() {
}
synchronized void m2() {
}
}
supposing two threads t1 and t2 have the same instance of A (a). t1 is calling a.m1() and t2 is calling a.m2().
My question is what would the behavior of t2 like when it calls m2 method and can't get the lock. Would t2 be suspended, or would t2 try to call m2 again later? Notice there's nothing of wait() or notify() in the synchronized method.
Thanks.
 
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
t2 would just pause until the lock became available. "suspended" has a specific technical meaning in Java, so you can't really say it's suspended, but yes, that's what it is.
 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying.
So does that mean the JVM will take care of the business, it will check the available of the lock for t2 and I don't need write anything like wait() or notify() in this case, is that right?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic