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.