Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronized block will lock the whole object or the method alone?

I have multiple methods in a class and most of the methods are having critical sections(shared data). So I made those methods as synchronized. Say thread t1 is running one of the synchronized block . At the same time thread t2 can access the other methods critical section ?

class Sample{

synchronized public void method1(){

}

synchronized public void method2(){

}

synchronized public void method3(){

}

public void method4(){

}

}
like image 223
JavaUser Avatar asked Jul 07 '17 06:07

JavaUser


1 Answers

synchronized is always locking on an object. In case of a synchronized method, the object is this. So basically these two methods do the same:

synchronized public void method1() {
  // do something
}

and

public void method1() {
  synchronized(this){
    // do something
  }
}

As long as one thread has the lock on the lock object, no other thread can lock this object. So in your example, the synchronized methods (one, two and three) can never be executed at the same time. method4 is not synchronized, so it can access the object anytime.

If you want a more fine-grained locking, because method1and method2 should be exclusive and method3and method4 you could use for example something like this:

class Sample{
  private final Object lock1 = new Object();
  private final Object lock2 = new Object();

  public void method1(){
    synchronized(lock1) {
      // do something
    }
  }
  public void method2(){
    synchronized(lock1) {
      // do something
    }
  }

  public void method3(){
    synchronized(lock2) {
      // do something
    }
  }
  public void method4(){
    synchronized(lock2) {
      // do something
    }
  }
}

You can then even use the synchonized(lock) method to just wrap the statements that need to be synchronized, not the whole method:

public void method() {
  // some code
  synchronized(lock) {
    // code that must be synchronized
  }
  // some other code
}

With this approach you can keep the lock duration to a minimum.

like image 112
P.J.Meisch Avatar answered Oct 13 '22 23:10

P.J.Meisch