How should the code below perform if executed randomly at different time?
public class Unstoppable extends Thread {
private int counter;
@Override
public void run() {
synchronized(this) {
for(int i = 0; i < 10; i++)
counter++;
this.notifyAll();
}
}
public static void main(String[] args) throws InterruptedException {
Unstoppable unStoppable = new Unstoppable();
System.out.println("I am about to start");
unStoppable.start();
synchronized(unStoppable) {
System.out.println("I was just told to wait");
unStoppable.wait();
}
System.out.println(unStoppable.counter);
}
}
At the first look at this it seemed like it would hang infinitely but quiety surprisingly every time I execute this...It completes execution.
wait
on a condition in loop. alsounstoppable unstoppable = new unstoppable();
does not follow proper naming convention for a class.