0

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().

OO7
  • 2,785
  • 1
  • 21
  • 33
Touchstone
  • 5,575
  • 7
  • 41
  • 48
  • You are expecting something from producer service, but not consuming it? So do future.get()? Also interested to know why you want to wait for main thread to co-exist? You could have the reference of thread if you save the reference of your producer/consumer instead of releasing them in execute call. – SMA Nov 25 '14 at 08:58
  • R u creating this in Web application ? – OO7 Nov 25 '14 at 09:10
  • @OO7 no, it is a normal standalone application. – Touchstone Nov 25 '14 at 09:12
  • What exactly ur problem definition is ? Will u update ur question with it ? So that it is more clear to answer. – OO7 Nov 25 '14 at 09:21
  • If the main thread has a task to perform, the task will affect whether and how you have to wait. But if the main thread has nothing to do, let it end. There is no benefit in blocking the associated resources. – Holger Nov 25 '14 at 09:40

4 Answers4

1

Why don't you try barriers?

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

PS. I think they work from JAVA7 and above

nansyg21
  • 61
  • 5
1

You can try with CountDownLatch. It will full fill your requirement, What you have to do is, create an instance of CountDownLatch with count value equal to the number of threads. Start all the thread and call await method in main thread. Have the CountDownLatch object reference in all the threads and call the countDown method in each thread after completing its own job. For each countDown method, the CountDownLatch count value will get decrement. Once it reaches to zero the method where await is called will get wake-up. Note if any one of your thread fails to call countDown then await will never get wake-up.

Try this for your reference, http://www.java-redefined.com/p/java-count.html

Jaya Ananthram
  • 3,433
  • 1
  • 22
  • 37
1

either you can use countdownlatch and wait for all the threads to complete execution or you can go for consumerService.awaitTermination() and producerService.awaitTermination();

Abhijeet Dhumal
  • 1,799
  • 13
  • 24
0

Probably a duplicate question to: How to wait for all threads to finish, using ExecutorService?

However, add below lines to your main method.

shutdown(); // you may want to handle SecurityException awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // handle exceptinos.

More: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

Community
  • 1
  • 1
Janakiram
  • 312
  • 1
  • 5
  • 12