During the execution of the main thread, you create two other threads and tell them to wait for each other. But you did not write anything so that your main thread waits for them and complains that it is not waiting. Try
CyclicBarrier barrier = new CyclicBarrier(3); mythread thread1 = new mythread(barrier).start(); mythread thread2 = new mythread(barrier).start(); barrier.await(); // now you wait for two new threads to reach the barrier. System.out.println("Should wait till both threads finish executing before printing this");
BTW. Do not extend the Thread class if necessary. Deploy Runnable and pass implementations to Thread objects. Like this:
class MyRunnable implements Runnable { public void run(){
EDIT
The rationale for preventing the spread of Thread.
A rule of thumb is combined as little as possible. Inheritance is a very strong link between classes. You must inherit from Thread if you want to change some of your default actions (for example, override some methods) or want to access some protected fields of the Thread class. If you do not want this, you select looser connection - implement Runnable and pass it as a constructor parameter to the Thread instance.
Tadeusz kopec
source share