The following code does not get notify the thread reader it execute the writer and then terminates. Why is it like that? The notifyall
should awake all the thread which are at the wait state.
public class Testing {
public static void main(String[] args) {
Testing testing=new Testing();
testing.reader.start();
testing.writer.start();
}
Thread reader = new Thread("reader") {
public void run() {
System.out.println("reader started");
synchronized (this) {
try {
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
for (int i = 0; i < 10; i++) {
System.out.println("reader " + i);
}
}
};
Thread writer = new Thread("writer") {
public void run() {
System.out.println("writer started");
for (int i = 0; i < 10; i++) {
System.out.println("writer " + i);
}
synchronized (Thread.currentThread()) {
notifyAll();
}
}
};
}