views:

378

answers:

5

What is a class level lock. Can you please explain with an example.

+3  A: 

If you use the synchronized keyword on a static method, the object whose monitor is used for the lock is the class object, i.e. the one represented by the literal MyClass.class. It is also used implicitly to syncronize during the initialization of the class itself.

Michael Borgwardt
+1  A: 

It's called an intrinsic lock, and every object has one. We need to separate two things, and it's not entirely clear what you mean by "class level lock".

Here's an example:

class Locker {
    public void x() {
        lock(this) { ... }
    }

    public void y() {
        lock(this) { ... }
    }
}

As you can see, both method uses "this" as a lock target - this way you can guarantee that they won't be ever interleaved. You can achieve this implicitly with the synchronization keyword:

class Locker {
    public synchronized void x() {
        ...
    }

    public synchornized void y() {
        ...
    }
}

The two examples are the same on the bytecode level.

However, by class lock, you could mean a lock on the actual class object - not an instance of it. That's how synchronized static methods work.

Zoltan
A: 
synchronized(MyClass.class){ ... }
KitsuneYMG
A: 

I am assuming you are referring to a synchronization lock. If you are unfamiliar with synchronization it is a means to prevent the same code being run by two different threads at the same time. In java synchronization is done with Object locks:

Object lock = new Object();

public void doSomething(){
  ...
  synchronized(lock){
    //something dangerous
  }
  ...
}

In this code it is guaranteed only one thread can do something dangerous at a given time, and it will complete everything in the synchronized block before another thread may start running the same code. I am guessing what you are referring to as a "class level lock" is the implicit lock on synchronized method:

public synchronized void somethingDangerous(){...}

Here again only one thread can execute the method at any given time and will always finish before another thread may begin executing the code. This is equivalent to a synchronized block on "this":

public void somethingDangerous(){
  synchronized(this){
    //something dangerous
  }
}

Now this lock isn't actually on the class, but rather on a single instance (i.e. not all clocks, but only your clock). If you want a true "class level lock" (which would typically be done in a static method), then you need to synchronize on something independent of any instances. For example:

public class Clock{
  private static Object CLASS_LOCK = new Object();

  public static void doSomething(){
    ...
    synchronized(CLASS_LOCK){
      //something dangerous to all clocks, not just yours
    }
    ...
  }
}

again there is an implicit lock for static methods:

public class Clock{
  public static synchronized void somethingDangerous(){}
}

which is the equivalent of locking on the class object:

public class Clock{
  public static void somethingDangerous(){
    synchronized(Clock.class){
      //do something dangerous
    }
  }
}
M. Jessup