aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object locks" Watch "Object locks" New topic
Author

Object locks

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Which of the following statements about threading are true
1) You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable
2) You can obtain a mutually exclusive lock on any object
3) A thread can obtain a mutually exclusive lock on a synchronized method of an object
4) Thread scheduling algorithms are platform dependent
Ans: 2,3,4
3???
2 makes sense. A lock is required by the thread from an object to enter any of its synch code, but not on a synch code by itself
Please correct me if it is one of those english twisters!!
Thankx
Ragu
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Ragu,
(2) you can obtain a lock on any object as the methods wait(), notify() and notifyAll() are implemented in java.lang.Object; the super class of every Java object.
The methods can only be called from synchronized code so 3 looks ok.

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
When you synchronize a method, 3 is what happens. Only the thread that owns the monitor for that method is allowed to access the method. Try re-reading answer 3, you must be misunderstanding what it is saying.


Darryl Failla
Sun Certified Java 2 Programmer
Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
Originally posted by Jane Griscti:
The methods can only be called from synchronized code so 3 looks ok.

This is not what the question is asking. It's asking whether you can have a lock on a synchronized method. When you invoke a synchronized method, you obtain a lock on the object. That lock prevents other threads from executing any syncronized method or synchronized code block from that object. To claim that onecan obtain a lock on a synchronized method is false. The lock is on the object. I say 3 is not okay.
Prasanna Wamanacharya
Ranch Hand

Joined: Apr 24, 2001
Posts: 143
Hi Ragu,
There are basically three levels of synchronization
Class-level
Single lock for all the object instances of the class.

Object-level
Tasks can call same method in different objects of the same class.
Method-level
Programmer specifies the methods which has to be synchronized.
I hope the above clears your doubt.
Prasanna
SCJP2.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Prassana
I just tried to figure out how to implement...

Class-level
Single lock for all the object instances of the class.
... synchronizing all the methods of the class on the same lock

Object-level
Tasks can call same method in different objects of the same class.

...sychronizing instances on different locks

Method-level
Programmer specifies the methods which has to be synchronized.

...obvious
But, could you give us an brief example of a situation where the first kind is intended to?


SCJP2. Please Indent your code using UBB Code
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
Jose:
1) Class-level: Single lock for all the object instances of the class.
==> If the method is declared static and synchronized, then the thread which wants to execute that method could acquire the class lock. Class level is provided by Java to synchronize the static methods and static variables.
Example:
class Test{
static i;
static synchronized method()
{
System.out.println( i );
}
If any thread wants to execute this method, then it has to acquire the class lock. i.e, lock of the class Test.
2) Object-level: Tasks can call same method in different objects of the same class.
Java provides lock for every object. If we have a class say, TestLock and when you create instances of that class, then each instance has a lock.
If a method in a class is declared synchronized in its declaration, then each thread has to acquire the lock of the current object.
If a part of the method in a class is preceded by synchronized( ObjectName), then that thread has to acquire the lock of that object.
If a part of the method in a class is preceded by synchronized( this), then that thread has to acquire the lock of current object.
Hope this helps...
Some one please do provide info about method-level...i did not hear about that so far...which is the good material to refer?


Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Umma
I thought Prassana was refering to something less obvious.

Class-level
Single lock for all the object instances of the class.

Any object of a class X can be used as a lock for all the instances of a class Y. We don't need the lock of an object of type Class for this. Besides that, an object of type Class is used generally for synchronizing only static methods while I was thinking that Prassana was referring to an object that is used to synchronize all the methods of a class. I wanted to know what was that scenario useful for.
Please Prassana can you clear my doubt. Because if my interpretation was wrong, I think that in Java the division between class-level, object-level and method level is not very useful. All the synchronization on Java is made on a method or block level. And always a portion of code is synchronized on an object. If you associate class-level synchronization with the lock being hold by a Class object, and object-level with the lock of any other type of object, as Umma is asking what is left for the method-level?
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
Hi Jose:
I thought that you are also asking for some examples provided by prasanna. I was confused how come Jose is asking this question...
bcoz i believed that you know the language in a good depth...
That's why i want to know what is the method-level lock...
Uma
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
IMHO method level is the standar way in which objects are synchronized in Java. There is no mystery here. You synchronized a method or a block. Maybe Prassana means:
a)class level: the object used for synchronization is of the type Class.b)object level: the lock is any other type of object.
But if this is what he meant I am still wondering the same as in my second parragraph in my previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object locks