I’m trying to understand how threads work, and I wrote a simple example where I want to create and start a new thread, the thread, display the numbers from 1 to 1000 in the main thread, resume the secondary thread, and display the numbers from 1 to 1000 in the secondary thread. When I leave out the Thread.wait()/Thread.notify() it behaves as expected, both threads display a few numbers at a time. When I add those functions in, for some reason the main thread’s numbers are printed second instead of first. What am I doing wrong?
public class Main {
public class ExampleThread extends Thread {
public ExampleThread() {
System.out.println("ExampleThread's name is: " + this.getName());
}
@Override
public void run() {
for(int i = 1; i < 1000; i++) {
System.out.println(Thread.currentThread().getName());
System.out.println(i);
}
}
}
public static void main(String[] args) {
new Main().go();
}
public void go() {
Thread t = new ExampleThread();
t.start();
synchronized(t) {
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i = 1; i < 1000; i++) {
System.out.println(Thread.currentThread().getName());
System.out.println(i);
}
synchronized(t) {
t.notify();
}
}
}
You misunderstand how
wait/notifyworks.waitdoes not block the thread on which it is called; it blocks the current thread until notify is called on the same object (so if you have threads A and B and, while in thread A, called B.wait(), this will stop thread A and not thread B – for as long as B.notify() is not called).So, in your specific example, if you want main thread to execute first, you need to put wait() inside the secondary thread. Like this:
However, even this code may not work like you want. In a scenario where the main thread gets to the notify() part before the secondary thread had a chance to get to the wait() part (unlikely in your case, but still possible – you can observe it if you put Thread.sleep at the beginning of the secondary thread), the secondary thread will never be waken up. Therefore, the safest method would be something similar to this:
In this example the output is completely deterministic. Here’s what happens:
tobject.tmonitor.tthread.tmonitor, the secondary thread cannot proceed and must wait (because its first statement issynchronized (this), not because it happens to be thetobject – all the locks, notifies and waits could as well be done on an object completely unrelated to any of the 2 threads with the same result.t.wait()part and suspends its execution, releasing thetmonitor that it synchronized on.tmonitor.t.notify(), waking the main thread. The main thread cannot continue just yet though, since the secondary thread still holds ownership of thetmonitor.t.wait(), suspends its execution and releases thetmonitor.tmonitor is now available.tmonitor but releases it right away.tmonitor.t.notify(), waking the secondary thread. The secondary thread cannot continue just yet, because the primary thread still holds thetmonitor.tmonitor and terminates.tmonitor, but releases it right away.As you can see, even in such a deceptively simple scenario there is a lot going on.