0

I am running two variations of similar code, expecting same result to appear however the output shows unexpected results.

variation1 with Synchronized block

public class SomeTask implements Runnable {

    public int  count = 0;
    
    @Override
    public void run() {

        synchronized (this) {
            for (int i = 0; i < 1_000_000; i++) {
                count++;
            }
        }
    }
}

public class TestMemory {
    
    public static void main(String[] args) {
        
        SomeTask task1 = new SomeTask();            
        Thread t1 = new Thread(task1);
        Thread t2 = new Thread(task1);

        t1.start();
        t2.start();
        
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main Thread Executed");
        System.out.println(Thread.currentThread().getName() + ": " + task1.count);
    }
}

output

Main Thread Executed main: 2000000

  • no matter how many times we run

variation2 with ReentrantLock

public class SomeTask implements Runnable {

    public int  count = 0;
    
    @Override
    public void run() {

        Lock lock = new ReentrantLock();
        try {
            lock.lock();
            for (int i = 0; i < 1_000_000; i++) {
                count++;
            }
        } finally {
            lock.unlock();
        }
    }
}

public class TestMemory {
    
    public static void main(String[] args) {
        
        SomeTask task1 = new SomeTask();
        SomeTask task2 = new SomeTask();
            
        Thread t1 = new Thread(task1);
        Thread t2 = new Thread(task1);

        t1.start();
        t2.start();
        
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main Thread Executed");
        System.out.println(Thread.currentThread().getName() + ": " + task1.count);
    }
}

output

Main Thread Executed main: 1023239

  • changes the value in multiple executions

Conclusion

Expected result is 2000000 in both the cases, however only synchronized block is working as expected.

samabcde
  • 6,988
  • 2
  • 25
  • 41

1 Answers1

0

Ah my bad I was using two different object of ReentrantLock Modified Code

public class TestMemory {
    
    public static void main(String[] args) {
        
        Lock lock = new ReentrantLock();
        SomeTask task1 = new SomeTask(lock);
        SomeTask task2 = new SomeTask(lock);
        
        
        Thread t1 = new Thread(task1);
        Thread t2 = new Thread(task1);

        t1.start();
        t2.start();
        
        
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main Thread Executed");
        System.out.println(Thread.currentThread().getName() + ": " + task1.count);

    }

}
----------

public class SomeTask implements Runnable {

    public int  count = 0;
    Lock lock;

    public SomeTask(Lock lock) {
        this.lock = lock;
        // TODO Auto-generated constructor stub
    }
    @Override
    public void run() {

        
        try {
            lock.lock();
            for (int i = 0; i < 1_000_000; i++) {
                count++;
            }
        } finally {
            lock.unlock();
        }

    }

}

Output

Main Thread Executed
main: 2000000
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501