Why n sometimes equals 1 or 2
private static int n = 0;
private static Thread t1, t2;
private synchronized static void increment() {
n++;
}
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
public void run() {
increment();
}
});
t2 = new Thread(new Runnable() {
public void run() {
t1.start();
increment();
}
});
t2.start();
try {
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(n);
}
Shouldn't the increment method only allow one thread to execute it at any given moment?
Maybe it is the debugger, it seems that when I run it normally I always get 2 but when i debug the code it some times return 1.
It does, however it could happen in either order. You only wait for t2 to finish but not for t1 to finish.
Wait for t1 as well.
private static int n = 0;
private static Thread t1, t2;
private synchronized static void increment() { // Lock will be on the "class" Object
n++;
}
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
public void run() {
increment();
}
});
t2 = new Thread(new Runnable() {
public void run() {
t1.start();
// t1 starts after t2. Now, t1's increment might also be called or t2's increment() might also be called. If t2 calls increment(), then the join() method below (you are joining the in the main thread) will be completed and "n" will be printed (if t1 is not getting appropriate time of execution..)
increment();
}
});
t2.start(); // t2 starts first
try {
t2.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(n); // increment() might not have been called by t1
}
There is no guarantee that one thread would execute before another (even with synchronized condition..). So, you can join on both t1 and t2 . This will ensure you will always get output as 2.
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