Hello everyone,


Using join can control how much time will we wait for a specific thread to accomplish its execution. I am meeting with a new scenario, which is a little different. My requirement is that I want my main thread starts several threads at the same time, which will perform their individual tasks in a concurrent way and I also want to control main thread waiting each thread for at most some time in a concurrent way. I am wondering how to "join" several threads in a concurrent way (traditional join function can only join one thread)?


Thanks in advance,
George

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).

Thanks jwenting,

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).

Your reply is very helpful. I am very interested in your chain trick but I do not quite understand what does it mean in detail. Can you show me more details dealing with your trick? Sample source codes or some online resources I can make a reference?


regards,
George

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.

Thanks jwenting,

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.

Your idea is very good but I think in your approach, all threads will work sequentially (NOT in a concurrrent way). Am I correct?

But my situation is that I want to join/wait several threads at the same time.

For example, I start 10 threads from main thread, and I want main thread wait each thread for at most 3 seconds. I do not want to join/wait each thread sequentially which will cost main thread 30 seconds to accomplish its task. I want main thread to wait the 10 threads in a concurrent way, which will cost main thread only 3 seconds to accomplish. Have I made myself understood?


regards,
George

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.

Thanks jwenting,

If the main thread has to wait 3 seconds on each thread it starts it will wait 30 seconds, period.

I want the main thread wait each thread for 3 seconds in a concurrent way. That means, the main thread only need 3 seconds to wait for the 10 threads, not 30 seconds. I mean waiting for each individual thread is performed in a concurrent way. Have I made myself understood?

Remember it's not guaranteed that any of those other threads will run...

Why it is not guaranteed? If I invoke start method of the threads, I think they ALL will be started. Am I correct?


regards,
George

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.

Thanks jwenting,

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.

Your reply is very helpful.


regards,
George

Thanks jwenting,

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.

I just want to rate your helpful thread so that it will benefit others, and hope you can continue to help me in the future.


regards,
George

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.