•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,598 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,266 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2459 | Replies: 9
![]() |
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Rep Power: 4
Solved Threads: 0
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
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 Date: Nov 2004
Location: Netherlands
Posts: 5,698
Reputation:
Rep Power: 18
Solved Threads: 195
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).
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).
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Rep Power: 4
Solved Threads: 0
Thanks jwenting,
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
•
•
•
•
Originally Posted by 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
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,698
Reputation:
Rep Power: 18
Solved Threads: 195
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Rep Power: 4
Solved Threads: 0
Thanks jwenting,
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
•
•
•
•
Originally Posted by 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
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,698
Reputation:
Rep Power: 18
Solved Threads: 195
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.
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.
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Rep Power: 4
Solved Threads: 0
Thanks jwenting,
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?
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
•
•
•
•
Originally Posted by 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?
•
•
•
•
Originally Posted by jwenting
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
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,698
Reputation:
Rep Power: 18
Solved Threads: 195
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.
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.
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Rep Power: 4
Solved Threads: 0
Thanks jwenting,
Your reply is very helpful.
regards,
George
•
•
•
•
Originally Posted by 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
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Rep Power: 4
Solved Threads: 0
Thanks jwenting,
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
•
•
•
•
Originally Posted by 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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: support mpeg4 in JMF player
- Next Thread: Retrieve key by value in Hashtable



Linear Mode