Question : How to preform Multi Thread Join Async

Hi I have a program that is running a few threads.
now i want to be able to run these few threads, and then to wait for all of them to end.

In my code sample you can see the code i am attempting but if i look at what actully happens, the join loop waits for the first thread to end only then it goes through the rest of the threads and joins them... what i would basiclly want to do, is let all the threads run and have a point where i know they all finished without calling finish on all of them because what would happen in a 3 threads example is this:

first iteration on joins:
th1 : isalive = false
th2 : isalive = true
th3 : isalive = true

second iteration on joins:

th1 : isalive = false
th2 : isalive = false
th3 : isalive = true

etc...

so what im looking for is how to wait for all them 3 to end without causing a while loop of busy/waiting on these threads..
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
//start threads
	for (int i=0;i<arrForagers.size();i++)
	{
		arrForagers.get(i).start();
	}	


//create join action for all the threads ************this is the problem*****************
for (int i=0;i<arrForagers.size();i++)
		{
		try {
			arrForagers.get(i).join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		}	
//from this point all the threads have completed successfully.

Answer : How to preform Multi Thread Join Async

order doesn't matter as join() will only return when that thread is finished
so you loop will only finish when all the threads are finished, regardless of what order they start or finish.

I've got an example of using CountdownLatch which I'll post for you, but the result will be the same as using join()
Random Solutions  
 
programming4us programming4us