The program I'm supposed to make a program where one thread starts three other threads. The original thread is going to wait until the three other has finished running and then the first thread can stop.
The way I've built the program the three threads is started from one class, let's call it FirstThread. The FirstThread class is started as a thread from main, and it only contains a run-method that starts up and shuts down the three threads. This seems to work in some way. But how do I make the first thread (started from main) to wait until the three other threads is finished before it finishes to?
Parts of the code:
Main:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main
{
public static void main(String[] args)
{
ExecutorService thread = Executors.newCachedThreadPool();
RingBuffer share = new RingBuffer();
thread.execute(new FirstThread(share));
thread.shutdown();
} // end main
} // end class main
The FirstThread:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FirstThread implements Runnable
{
ExecutorService thread = Executors.newCachedThreadPool();
RingBuffer share = new RingBuffer();
public FirstThread(Buffer share)
{
this.share = share;
} // end constructor
public synchronized void run()
{
share.outPut("Test");
thread.execute(new Son1(share));
thread.execute(new Son2(share));
thread.execute(new Son3(share));
thread.shutdown();
} // end method
} // end class