Why this program doesn`t display 2000 at every execution? I know that I can use AtomicInteger, but I am curious.
class Increment extends Thread{
static Integer i=new Integer(0);
public void run(){
for(int j=1;j<=1000;j++){
synchronized (i) {
i++;
}
}
}
}
public class Puzzle {
public static void main(String args[]) {
Thread t1=new Increment();
Thread t2=new Increment();
t1.start();
t2.start();
try {
t1.join();
t2.join();
}catch (InterruptedException r){}
System.out.println(Increment.i);
}
}
You synchronize on a mutable variable i
. This variable changes its value each time, therefore each time you acquire a lock on another object. Each thread thus acquires a non-contended lock and can proceed simultaneously, as if no synchronization was in place.
Lesson: use a dedicated private static final Object lock = new Object()
as a lock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With