This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Synchronized method behavior 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 "Synchronized method behavior" Watch "Synchronized method behavior" New topic
Author

Synchronized method behavior

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
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?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Right.


"I'm not back." - Bill Harding, Twister
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Synchronized method behavior
 
Similar Threads
synchronization without wait and notify
synchronization
Multithreading
Thread behavior with synchronized method
Static and non-static synchronized method