3

I want to print an array only after 2 threads that I created are finish run method. How can iI do that?

Perception
  • 79,279
  • 19
  • 185
  • 195
user2097810
  • 735
  • 6
  • 17
  • 25

6 Answers6

6

Take a look at the method Thread#join(). For example:

Thread a = ...;
a.start();
a.join(); // wait until Thread a finishes
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
6

Simple. Use Thread.join(). While you are spawning your threads add them into a list and loop through that list and call thread.join(). Once you get out of that loop, all your threads are confirmed to be finished. Then you can have the print statement after that.

Something like this:

import java.lang.*;

public class ThreadDemo implements Runnable {

   public void run() {

      //some implementation here
   }

   public static void main(String args[]) throws Exception {

      List<Thread> threadList = new ArrayList<Thread>();
      Thread t1 = new Thread(new ThreadDemo());
      t1.start();
      threadList.add(t1);
      Thread t2 = new Thread(new ThreadDemo());
      t2.start();
      threadList.add(t2);
      for(Thread t : threadList) {
          // waits for this thread to die
          t.join();
      }
      System.out.print("All the threads are completed by now");
   }
}
Amar
  • 11,930
  • 5
  • 50
  • 73
1

Have you tried anything?

The standard way of having code wait for a thread to finish is to call the join() method on that thread; when that returns, the thread is done. Try looking that up and seeing what you can figure out.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • i just want to print variable -array, after c1 and c2 finish run methood. c1.start(); c2.start(); System.out.println(array); – user2097810 Mar 11 '13 at 20:13
0

You could submit these jobs to an Executor, each of them will return a Future object. Call the get() method on each of these futures and you'll block until all of them have completed:

String[] myArr = new String[0];
ExecutorService service = Executors.newSingleThreadExecutor();

//Just one task, but repeat with as many as needed.
Future f = service.submit(new Runnable() {
    public void run() {
        //Executing code
    }
});
f.get();

System.out.println(Arrays.toString(myArr));  //Print array.

Thread.join() is the more standard way to wait until a particular thread has completed, but personally in this day and age I prefer this approach - it makes it much easier to say swap out the single threaded executor for a concurrent thread pool (or similar) later should the need arise, and personally I find it neater too. It can also be easily refactored to work with Callables, providing a Future which can directly get the result of the concurrent computation.

Either approach will work, the one which is better for you will depend on your use case.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Well, berry's answer is more modern. All my thread work was done before these new-fangled classes. Mine probably still works, his is more up-to-date. – arcy Mar 11 '13 at 20:12
0

Take a look to this post (How to wait for a set of threads to complete?, How to wait for a number of threads to complete?).

Community
  • 1
  • 1
Daniel García Baena
  • 1,191
  • 4
  • 19
  • 33
0

My opinion is that you should use CountDownLatch.

Before printing you should show this:

CountDownLatch startSignal = new CountDownLatch(2);

// Create your threads and add startSignal as parameters to them

At the end of each thread you shoulf call:

startSignal.countDown();

After that before print you should call:

startSignal.await();
// print...

This will continue after counter reaches zero.

partlov
  • 13,789
  • 6
  • 63
  • 82