Results 1 to 3 of 3
  1. #1
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Using separate locks has no effect

    Java Code:
    public class SCounter {
        private int c = 0;
      	Object lock1=new Object();
      	Object lock2=new Object();
    
        public void increment() {
          synchronized(lock1) {
            c++;
          }
        }
    
        public synchronized void decrement() {
          synchronized(lock2) {
            c--;
          }
        }
    
        public synchronized int value() {
            return c;
        }
    }
    
    public class Run1 implements Runnable {
      SCounter sc;
      
      public Run1(SCounter sc) {
        this.sc=sc;
      }
      
      public void run() {
        sc.increment();
        sc.increment();
        sc.increment();
        System.out.println(sc.value());
      }
    }
    
    public class Run2 implements Runnable {
      SCounter sc;
      
      public Run2(SCounter sc) {
        this.sc=sc;
      }
      
      public void run() {
        sc.decrement();
        sc.decrement();
        sc.decrement();
        System.out.println(sc.value());
      }
    }
    
    public class SCMain {
      public static void main(String[] args) {
        SCounter sc = new SCounter();
        new Thread(new Run1(sc)).start();
        new Thread(new Run2(sc)).start();
      }
    }
    synchronized(lock1) and synchronized(lock2)
    has the same effect as
    synchronized(lock1) and synchronized(lock1)

    what's the point?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Using separate locks has no effect

    In the end you're just doing +3 and -3. Whatever execution order there is, locks or no locks, 0+3-3 wil always be 0.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Using separate locks has no effect

    Your test case is not elaborate enough to ever generate an error. I modified your example to show when it goes wrong. Consider the Thread.sleep() as a "latency generator". Switch the lock on one thread to see when it goes out of sync.

    After incrementing/decrementing, there is a little pause. As soon as both threads use lock1, there is no problem, but using lock1 and lock2, the counter goes out of sync.

    Java Code:
    import java.util.Random;
    
    public class Locks {
        public static void main(String[] args) {
            SCounter sc = new SCounter();
            new Thread(new Run1(sc), "Increment").start();
            new Thread(new Run2(sc), "Decrement").start();
        }
    }
    
    class SCounter {
        private int c = 0;
        Object lock1 = new Object();
        Object lock2 = new Object();
    
        public void increment() {
            c++;
        }
    
        public void decrement() {
            c--;
        }
    
        public int value() {
            return c;
        }
    
    }
    
    class Run1 implements Runnable {
        Random random = new Random();
        SCounter sc;
    
        public Run1(SCounter sc) {
            this.sc = sc;
        }
    
        public void run() {
            for (int i = 1; i <= 100; i++) {
                try {
                    Thread.sleep(random.nextInt(100) + 200);
                }
                catch (InterruptedException e) {
                }
    
                synchronized (sc.lock1) {
                    int oldValue = sc.value();
                    sc.increment();
                    try {
                        Thread.sleep(random.nextInt(100) + 200);
                    }
                    catch (InterruptedException e) {
                    }
                    int newValue = sc.value();
                    if (newValue != oldValue + 1) {
                        System.out.println(Thread.currentThread().getName() + " " + i + ": expected " + (oldValue + 1) + ", got " + newValue);
                    }
                    else {
                        System.out.println(Thread.currentThread().getName() + " " + i + ": " + newValue);
                    }
                }
            }
    
        }
    }
    
    class Run2 implements Runnable {
        Random random = new Random();
        SCounter sc;
    
        public Run2(SCounter sc) {
            this.sc = sc;
        }
    
        public void run() {
            for (int i = 1; i <= 100; i++) {
                try {
                    Thread.sleep(random.nextInt(100) + 200);
                }
                catch (InterruptedException e) {
                }
    
    
                synchronized (sc.lock2) {
                    int oldValue = sc.value();
                    sc.decrement();
                    try {
                        Thread.sleep(random.nextInt(100) + 200);
                    }
                    catch (InterruptedException e) {
                    }
                    int newValue = sc.value();
                    if (newValue != oldValue - 1) {
                        System.out.println(Thread.currentThread().getName() + " " + i + ": expected " + (oldValue - 1) + ", got " + newValue);
                    }
                    else {
                        System.out.println(Thread.currentThread().getName() + " " + i + ": " + newValue);
                    }
                }
            }
    
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Reentrant locks and JDBC
    By nuperibo in forum Advanced Java
    Replies: 0
    Last Post: 02-02-2014, 01:47 PM
  2. Synchronization utility to manage locks
    By JSL5101 in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2011, 05:43 PM
  3. Problems using wait() and notifyAll() with locks
    By cuffJ in forum Threads and Synchronization
    Replies: 6
    Last Post: 08-18-2010, 03:21 AM
  4. InterruptedException and Locks
    By JavaJuJitZu in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-19-2010, 02:47 AM
  5. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •