• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class level lock and object level lock

 
Balbhadra Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, both methods need to be synchronized of course:
class Foo{
static synchronized void staticMethod(){}
synchronized void instanceMethod(){}
}
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gaurava Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve for the explanation, it clears my doubt.

I will follow the Zombie policy from now on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic