File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes class level lock and object level lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "class level lock and object level lock" Watch "class level lock and object level lock" New topic
Author

class level lock and object level lock

Balbhadra Singh
Greenhorn

Joined: Oct 14, 2003
Posts: 13
Hi,
Can someone throw some light on the behaviour of class/object level monitor in java?

Does one takes precedence over other?

What happens when, class has both synchronized instance methods and synchronized static methods? Does calling thread has to acquire both class/object locks?

I would welcome a detailed explaination.

Thanks in advance,
Balbhadra
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Hi,
Those locks are not related, and each needs to be obtained regardless of the other.
Namely, if you have:
class Foo{
static void staticMethod(){}
void instanceMethod(){}
}
And then an instance: Foo f=new Foo()
Then you have 2 unrelated monitors (one for class Foo, one for instance f ). Any threads attempting to invoke staticMethod() will need to gain access to the (single) class monitor, and nothing else. Any threads calling f.instanceMethod() will need to gain access to "f" monitor, and nothing else.
If you need any access hierarchy, you'll need to do it programatically, which may look as follows (however, beware - such nested locks pose some dangers of deadlocks unless used with care):
synchronized(Foo.class){
synchronized(f){
// my code
}
}
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Sorry, both methods need to be synchronized of course:
class Foo{
static synchronized void staticMethod(){}
synchronized void instanceMethod(){}
}
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Hi,

This is from JLS:

A synchronized method acquires a lock (�17.1) before it executes. For a class (static) method, the lock associated with the Class object for the method's class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used.

class Test {
int count;
synchronized void bump() { count++; }
static int classCount;
static synchronized void classBump() {
classCount++;
}
}

has exactly the same effect as:


class BumpTest {
int count;
void bump() {
synchronized (this) {
count++;
}
}
static int classCount;
static void classBump() {
try {
synchronized (Class.forName("BumpTest")) {
classCount++;
}
} catch (ClassNotFoundException e) {
...
}
}
}

I assume that since we r dealing with 2 different locks then Class lock will lock all static synch methods and 'this' lock will lock all synch instance methods.

What do you think?
Gaurava Agarwal
Greenhorn

Joined: Aug 10, 2009
Posts: 4
I am concerned about how class lock works. As per standard behaviour of synchronization, " only one synchronized static method can be called at a time "

please correct me if am wrong.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Gaurava Agarwal wrote:I am concerned about how class lock works. As per standard behaviour of synchronization, " only one synchronized static method can be called at a time "

please correct me if am wrong.


Hi Guarava,

Welcome to JavaRanch, it is good to see you here :-)

We have a standard policy here called Don't Wake The Zombies, basically, rather than wake up a 5 year old thread with your question, your question deserves its own Thread. Searching is great, we Definitely Encourage Searching but if you don't find what you want, you will probably get better results with a new Thread rather than an old one.

Anyway, to your question. You ask if only one synchronized static method can be called at a time. The answer is yes, for a given Class, only one synchronized static method can be called at a time. Synchronized static methods of different Classes, however, can be called at the same time, because the lock is on different instances of the Class object.

Does that help?


Steve
Gaurava Agarwal
Greenhorn

Joined: Aug 10, 2009
Posts: 4
Thanks Steve for the explanation, it clears my doubt.

I will follow the Zombie policy from now on!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class level lock and object level lock