My Code snippet:
public class ProducerConsumerTest {
public static void main(String[] args)
{
ArrayBlockingQueue<String> sharedQueue = new ArrayBlockingQueue<String>(20, true);
ExecutorService consumerService = Executors.newFixedThreadPool(5);
ExecutorService producerService = Executors.newSingleThreadExecutor();
Future future=producerService.submit(new Producer(sharedQueue));
consumerService.execute(new Consumer(sharedQueue));
consumerService.execute(new Consumer(sharedQueue));
consumerService.execute(new Consumer(sharedQueue));
consumerService.execute(new Consumer(sharedQueue));
consumerService.execute(new Consumer(sharedQueue));
}
}
How to make the main thread wait for other threads ?
The problem I am facing is that I don't know the name of the threads created by ExecutorService
, that's why I cannot use join()
. eg: t1.join()
in main()
.