join will make one thread wait until another completes. You can chain this (by having a joined thread join another thread itself), but it gets tricky (and if you do, why use threads at all as you're effectively moving away from concurrent processing and back to a linear application flow).
You cannot have one thread join several others at the same time for the simple reason that the action of joining makes the thread wait on the other thread (and therefore you cannot have it join something else again until the join is ended for whatever reason).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
if you have 3 threads, you can have #1 join on #2 and #2 join on #3. #1 now won't complete until #2 completes but that one won't complete until #3 completes so #1 won't complete until #3 completes.
Like I said it's risky because you could join #3 on #1 and everything would wait forever.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
If the main thread has to wait 3 seconds on each thread it starts it will wait 30 seconds, period.
Remember it's not guaranteed that any of those other threads will run...
It might be better to have a thread set some flag to indicate it's completed and have the main thread poll those flags in a loop (with a sleep of say 0.1 second in each iteration) to determine whether all child threads are done.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
you wait 3 seconds total for 10 threads then you have a completely different effect from when you wait 3 seconds for each thread.
Remember that those threads will NOT all execute at the same time so if you wait the proper time for one thread you will AT MOST have one thread completed during that time.
That is, unless you are running on a multi-CPU machine with enough CPUs to run each thread in its own and have a JVM that will actually do that.
Start places a thread in runnable mode, it does NOT actually cause the thread to run.
It just tells the JVM that "yes, this thread is now ready to run, please see that it gets some CPU time when it pleases you".
The JVM will then at some point in the future schedule that thread for execution. Depending on JVM implementation and the actual code of your program and the OS you're running on this may happen immediately, only after there are no other threads, or at some point in between.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337