File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread lock (monitor) question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread lock (monitor) question" Watch "thread lock (monitor) question" New topic
Author

thread lock (monitor) question

James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
refering to JLS 17.1, one Object only has one monitor. The monitor can be held by only one thread at a time. If a thread wants to access synchronized block, it must obtain the monitor.
Does that imply if a class has 2 synchronized methods (say methodA(), methodB()), if thread-1 is accessing methodA(), no any other thread can access methodB(), even though thread-1 is only accessing methodA() not methodB()?
[ October 14, 2006: Message edited by: James Quinton ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

Originally posted by James Quinton:
Does that imply if a class has 2 synchronized methods (say methodA(), methodB()), if thread-1 is accessing methodA(), no any other thread can access methodB(), even though thread-1 is only accessing methodA() not methodB()?


Forgetting about static methods for the moment... yes, that is correct. When a synchronized method is executing, all other threads will have to wait to access *any* synchronized methods.

Static methods are a bit different, since there is no "this" object, they use the class object instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Y Enev
Greenhorn

Joined: Oct 14, 2006
Posts: 8
This is the case if both methodA and methodB are non-static and they are invoked using the same instance. If one of the methods is static though, they can be called by two different threads.
The difference is that if a thread gets the lock of static synchronised methods it actually get the lock of the class. If the method is non-static the thread will get the lock of the object that called the synchronised method.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Referring to the previous post:
"If the method is non-static the thread will get the lock of the object that called the synchronised method."

You sure about that?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Y Enev
Greenhorn

Joined: Oct 14, 2006
Posts: 8
Threads calling non-static synchronized methods in the same class will only block each other if they're invoked using the same instance. That's because they each lock on this instance, and if they're called using two different instances, they get two locks, which do not interfere with each other.


This is what is says in "SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055)" book by Kathy Sierra and Bert Bates. I just tried to say the same using fewer words.



[HENRY: Fixed quote tags]
[ October 14, 2006: Message edited by: Henry Wong ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
To "Y",
the point I picked you up on was that you wrote "get the lock of the object that called the synchronised method". If a thread that is executing code in my object calls a synchronized method in the class of another object, the thread has to get the lock on the other object, not my own.

Did you mean "contains" the method rather than "called" the method?
[ October 15, 2006: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread lock (monitor) question