You misunderstand how wait
/notify
works. wait
does 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:
public class Main {
public class ExampleThread extends Thread {
public ExampleThread() {
System.out.println("ExampleThread's name is: " + this.getName());
}
@Override
public void run() {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
}
}
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();
for(int i = 1; i < 1000; i++) {
System.out.println(Thread.currentThread().getName());
System.out.println(i);
}
synchronized(t) {
t.notify();
}
}
}
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:
public class Main {
public class ExampleThread extends Thread {
public ExampleThread() {
System.out.println("ExampleThread's name is: " + this.getName());
}
@Override
public void run() {
synchronized (this) {
try {
notify();
wait();
} catch (InterruptedException e) {
}
}
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();
synchronized (t) {
t.start();
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();
}
}
}
In this example the output is completely deterministic. Here's what happens:
- Main thread creates a new
t
object.
- Main thread gets a lock on the
t
monitor.
- Main thread starts the
t
thread.
- (these can happen in any order)
- Secondary thread starts, but since main thread still owns the
t
monitor, the secondary thread cannot proceed and must wait (because its first statement is synchronized (this)
, not because it happens to be the t
object - 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.
- Primary thread continues, gets to the
t.wait()
part and suspends its execution, releasing the t
monitor that it synchronized on.
- Secondary thread gains ownership of
t
monitor.
- Secondary thread calls
t.notify()
, waking the main thread. The main thread cannot continue just yet though, since the secondary thread still holds ownership of the t
monitor.
- Secondary thread calls
t.wait()
, suspends its execution and releases the t
monitor.
- Primary thread can finally continue, since the
t
monitor is now available.
- Primary thread gains ownership of the
t
monitor but releases it right away.
- Primary thread does its number counting thing.
- Primary thread again gains ownership of the
t
monitor.
- Primary thread calls
t.notify()
, waking the secondary thread. The secondary thread cannot continue just yet, because the primary thread still holds the t
monitor.
- Primary thread releases the
t
monitor and terminates.
- Secondary thread gains ownership of the
t
monitor, but releases it right away.
- Secondary thread does its number counting thing and then terminates.
- The entire application terminates.
As you can see, even in such a deceptively simple scenario there is a lot going on.