-5

Could someone explain the difference between synchronized(this) and synchronized (c)?

I refereed various answers(1,2,3,4,5) but not able to understand and getting confused.

public class Check {

    Consumer c = new Consumer(null);

    public void getSynch() {

        synchronized (this) {
            // doing some task
            System.out.println("Start");
        }

        synchronized (c) {
            // doing some task
            System.out.println("End");
        }

    }

}

I am able to understand the synchronized concept but not monitor object. Please explain in a simple way.

Partha
  • 402
  • 4
  • 15
J_Coder
  • 707
  • 5
  • 14
  • 32

1 Answers1

1

synchronized always works on a monitor object (sometimes also called lock or semaphore). Think of it as a token: when a thread enters a synchronized block it grads the token and other threads need to wait for the token to be returned. If you have multiple different monitor objects you basically have different tokens and thus synchronized blocks that operate on different monitors can run in parallel.

There are many possibilities with this and many possible use cases. One could be that multiple instances of a class could run in parallel but need access to a shared and non-threadsafe resource. You then could use that resource or any other object that represents the "token" for that resource as the monitor object.

However, note that there's potential for deadlocks, e.g. in the following situation:

Block 1:

synchronized(A) {
  //do something with A
  synchronized(B) {
    //do something with B
  }
}

Block 2:

synchronized(B) {
  //do something with B
  synchronized(A) {
    //do something with A
  }
}

Here both outer synchronized blocks could be entered in parallel because the two monitors A and B are available but then need to grab the other monitor and because they are now locked both threads would have to wait - class deadlock.

Also have a look at the dining philosophers problem which handles that topic as well (here the forks could be considered the monitor objects).

Edit:

In your case (the code you've posted), multiple threads could try to call getSynch() on the same instance of Check. The first block synchronizes on the instance itself thus preventing multiple threads from entering that block if called on the same instance.

The second block synchronizes on c which is a different object and could change over time. Assume the first block (synchronized(this) { ... }) changes c to reference another instance of Consumer. In that case you could have multiple threads run that block in parallel, e.g. if one entered the synchronized(c) block before the other thread reassigns c.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • What does 'this' as a parameter here? only the instances of Check thread alone should wait once the current thread(Check) done? – J_Coder Nov 06 '18 at 09:49
  • `this` means that the current instance of `Check` will be used as the monitor. In theory multiple threads could call `getSynch()` on the _same_ instance (`Check` itself is _not_ a thread btw). – Thomas Nov 06 '18 at 09:52