import java.util.concurrent.atomic.AtomicBoolean;
class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
try {
System.out.println("11111111");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void decrement() {
c--;
}
public synchronized int value() {
return c;
}
}
public class test2 extends Thread{
static SynchronizedCounter c = new SynchronizedCounter(); ;
public void run(){
c.increment();
}
public static void main(
String[] args) {
test2 t = new test2();
test2 t1 = new test2();
t.start();
t1.start();
}
}
The result-----
11111111
11111111
Q if the SynchronizedCounter methods are synchronized then I can assume that when one
thread enters the critical section in increment() then the other thread is blocked. then why 11111 is printed twice instead of once??