1

I was reading on official tutorial on multithreading from oracle and I came cross this example (assuming c1 and c2 are never used together):

public class MsLunch {
    private long c1 = 0;
    private long c2 = 0;
    private Object lock1 = new Object();
    private Object lock2 = new Object();

    public void inc1() {
        synchronized(lock1) {
            c1++;
        }
    }

    public void inc2() {
        synchronized(lock2) {
            c2++;
        }
      }
    }
}

It is said that by using lock 1 & lock 2, it helps to reduce unnecessary blocking as compare to using the word this in the synchronized block.

However, I don't really see how this helps to reduce blocking as they have no dependency on each other. I have multiple threads each running these two methods at the same time and the performance is rather similar when I use the lock objects and this keyword.

Can someone help to explain my confusion here? Love to see the explanation with an example to illustrate the difference clearly.


Adding on to the discussion here, this post helped to clarify my doubts as well. Key point: Putting synchronized on a method means the thread has to acquire the lock on the object instance before entering that method

Community
  • 1
  • 1
hao
  • 635
  • 2
  • 8
  • 20
  • To see the performance difference in this case, you would have to make a large number of calls using a large number of threads calling your 2 methods randomly and concurrently. However, if the calls did more than increment a variable then the different between the 1 versus 2 locks would be more evident. – Gray May 24 '16 at 19:56

1 Answers1

3

You are using two different locks - one to protect inc1 and a different one to protect inc2. This means that thread X can run inc1 while another thread is running inc2. Had they used the same lock (regardless whether it's this or a different lock object), you would not be able to run them both concurrently. Thus, in theory at least, having two different locks should increase your performance in this scenario.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I understand your point here. I guess my confusion is that if c1 and c2 are being updated by two methods separately in this case, would having two different locks affect the performance as compare to using only one lock. From your answer, I think you are saying that synchronized blocks using same lock would not run concurrently - in this case c2 will not be updated if c1 is being updated on different threads? – hao May 24 '16 at 14:54
  • @hao exactly - that's why we have locks - to prevent one thread from interfering with another, regardless of whether they're running the same method or not. – Mureinik May 24 '16 at 14:56
  • 1
    The point is, since you are trying to protect two independent variables, you _can_ use two independent locks. If thread A is in a call to `inc1()`, that need not be a reason to block thread B from entering `inc2()`. – Solomon Slow May 24 '16 at 14:57
  • 2
    Thank you, synchronzied blocks with the same lock will all be locked when one of the blocks are accessed by one thread. Is this the right way to understand it? – hao May 24 '16 at 15:00
  • @hao yup, exactly. – Mureinik May 24 '16 at 15:03