Results 1 to 3 of 3
- 04-18-2017, 12:16 PM #1
Member
- Join Date
- Dec 2016
- Posts
- 13
- Rep Power
- 0
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(); } }
has the same effect as
synchronized(lock1) and synchronized(lock1)
what's the point?
- 04-18-2017, 12:38 PM #2
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
- 04-18-2017, 01:00 PM #3
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
-
Reentrant locks and JDBC
By nuperibo in forum Advanced JavaReplies: 0Last Post: 02-02-2014, 02:47 PM -
Synchronization utility to manage locks
By JSL5101 in forum Advanced JavaReplies: 1Last Post: 05-07-2011, 06:43 PM -
Problems using wait() and notifyAll() with locks
By cuffJ in forum Threads and SynchronizationReplies: 6Last Post: 08-18-2010, 04:21 AM -
InterruptedException and Locks
By JavaJuJitZu in forum Threads and SynchronizationReplies: 3Last Post: 02-19-2010, 03:47 AM -
Should I separate my code into separate files?
By Inks in forum New To JavaReplies: 0Last Post: 03-26-2009, 01:12 AM
Bookmarks