19

Let's say I've got this very simple code:

for(int i = 0; i < 10; i++) { 
    thread = new Thread(this); 
    thread.start(); 
} 

However, in this code, the thread apparently starts 10 times at once and it doesn't wait before the previous one is finished. How do you check if the thread is finished before letting the thread start again?

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
ZimZim
  • 3,291
  • 10
  • 49
  • 67
  • 3
    Why are you starting a thread if you don't want to do anything until that thread is complete? It would be easier to just do a regular method call, which won't return until it has finished. – goto10 Nov 27 '11 at 19:18
  • Seems duplicate: https://stackoverflow.com/questions/289434/how-to-make-a-java-thread-wait-for-another-threads-output – akhil_mittal Apr 01 '17 at 06:39

3 Answers3

35

Before answering your question, I strongly encourage you to look into ExecutorServices such as for instance the ThreadPoolExecutor.

Now to answer your question:

If you want to wait for the previous thread to finish, before you start the next, you add thread.join() in between:

for(int i = 0; i < 10; i++) { 
    thread = new Thread(this); 
    thread.start(); 

    thread.join();    // Wait for it to finish.
}

If you want to kick off 10 threads, let them do their work, and then continue, you join on them after the loop:

Thread[] threads = new Thread[10];
for(int i = 0; i < threads.length; i++) { 
    threads[i] = new Thread(this); 
    threads[i].start(); 
}

// Wait for all of the threads to finish.
for (Thread thread : threads)
    thread.join();
Aaron Blenkush
  • 3,034
  • 2
  • 28
  • 54
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • This makes the main thread wait until the launched thread is finished. If I understood correctly, the OP wants each launched thread wait until the previous one is finished. – JB Nizet Nov 27 '11 at 19:21
  • 1
    On the other hand, why would he create 10 threads in the first place if he intended to do 10 things sequentially? – aioobe Nov 27 '11 at 19:58
  • 1
    Why would you do that? I can provide a use case of mine :) you would to that if you need to do a series of network connections in android and you want to avoid NetworkOnMainThreadException – ocramot Apr 14 '15 at 15:32
  • this answer saved my day – TomInCode Dec 21 '20 at 23:18
11

If every thread must wait for the previous one to finish before starting, you'd better have one unique thread executing the original run method 10 times in sequence:

Runnable r = new Runnable() {
    public void run() {
        for (int i = 0; i < 10; i++) {
            OuterClass.this.run();
        }
    }
}
new Thread(r).start();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

Just to elaborate on aioobe's suggestion:

Before answering your question, I strongly encourage you to look into ExecutorServices such as for instance the ThreadPoolExecutor.

There is a particular ExecutorService that can be used for this task:

ExecutorService pool = Executors.newSingleThreadExecutor();
for (int i=0; i<10; i++) {
  pool.submit(this); //assuming this is a Runnable
}
pool.shutdown(); //no more tasks can be submitted, running tasks are not interrupted

newSingleThreadExecutor() is similar to calling newFixedThreadPool(1) but ensures that the service cannot be reconfigured to use more than one thread.

crnv
  • 316
  • 2
  • 14