The second thread is not created correctly:
Thread t2 = new Thread(t1);
I cannot support it with the documentation, but in the source code of Thread.run() I see:
if (target != null) { target.run(); }
Where is the target instance of Runnable . When Thread is executed, it clears the target variable:
private void exit() {
This means that when the first thread executes (the join() method), it clears the target , and the second thread does nothing. When join() is removed, both access the same target from t1 (race condition).
TL DR
Never create a stream using another instance of the stream (although it implements Runnable ). Instead, create a separate Runnable and pass it:
final Runnable run = new Runnable() { public void run() { System.out.println(Thread.currentThread().getName()); } }; Thread t1 = new Thread(run, "t1"); Thread t2 = new Thread(run, "t2");
You don't need join() , by default it's non-daemon threads.
see also
source share