• 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

Object locks

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic