0

I have a requirement in multi-threaded environment in java. The problem is like;

I have suppose 10 different task, and I want to assign all these 10 task to 10 different threads. Now the finish time for these tasks could be different. And there is some finishing or clearance task which should be performed when all these 10 threads are finished. In other words i need to wait until all threads are finished and then only I can go ahead with my further code execution.

Please let me know if any more details required here.

Thansk, Ashish

Ashish
  • 402
  • 2
  • 6
  • 15
  • http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html – dinesh707 Nov 18 '13 at 07:40
  • Check out http://stackoverflow.com/questions/289434/how-to-make-a-java-thread-wait-for-another-threads-output – Birb Nov 18 '13 at 07:41

4 Answers4

1

CyclicBarier (JDK java.util.concurrent) of size 10 is perfect solutuon for you. With CyclicBarier you can wait for 10 threads. If all t hreads achieve barier then you can go further.

Edit: CyclicBarier is almost the same as CountDownLatch but you can reuse barier invoking reset() method.

Areo
  • 928
  • 5
  • 12
1

Sounds like an ideal job for CountDownLatch.

Initialize it with 10 counts and when each thread finishes its job, it counts down one. When all 10 threads have finished, the CountDownLatch will let the original thread run, and it can perform the cleanup.

And fire up an ExecutorService with 10 fixed threads to run the tasks.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

If each thread terminates after it is finished, you could just use the join() statement. A simple example can be found in the Essential Java Tutorials.

    ArrayList<Thread> myThreads = new ArrayList<Thread>();
    for (int i = 0; i < 10; i++){
        //MyTaskRunnable is a Runnable with your logic
        Thread t = new Thread(new MyTaskRunnable());
        myThreads.add(t);
    }
    for(Thread t : myThreads){
        t.start();
    }
    //here all threads are running

    for(Thread t : myThreads){
        t.join();
    }
    //here all threads have terminated

Edit: The other answers all have their merits and are very useful in practice, the join() is however the most basic of the constructs. The CyclicBarrier and CountDownLatch versions allow your threads to continue running after reaching the synchronization point, which can be necessary in some cases. The ExecutorService is more suited to many tasks needing to be executed on a fixed number of threads (aka a thread pool), to create an ExecutorService for just 10 tasks is a bit drastic.

Finally, if you are new to learning Java or are taking a course on concurrency, you should try out all the variants and see what they do. The join is the most basic of these constructs and will help you understand you what is going on. Also it is the basic model supported by most other languages.

jmiserez
  • 2,991
  • 1
  • 23
  • 34
0

Whilst CountDownLatch and CyclicBarier do the job of synchronizing multiple threads and performing one action when all threads reach the required point, they require all tasks to actively use this feature. If you are interested in the finishing of the entire task(s) only, the solution can be much simpler: add all tasks to a Collection and use the invokeAll method of an ExecutorService which returns when all tasks have been completed. A simple example:

Callable<Void> simpleTask=new Callable<Void>() {
  public Void call() {
    System.out.println("Performing one job");
    return null;
  }
};
List<Callable<Void>> list = Collections.nCopies(10, simpleTask);
ExecutorService es=Executors.newFixedThreadPool(10);
es.invokeAll(list);
System.out.println("All completed");
Holger
  • 285,553
  • 42
  • 434
  • 765