User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,018 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 1,711 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: 2540 | Replies: 9
Reply
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Question about join of thread.

  #1  
Nov 12th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Question about join of thread.

  #2  
Nov 12th, 2004
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).
Reply With Quote  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Question about join of thread.

  #3  
Nov 14th, 2004
Thanks jwenting,


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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Question about join of thread.

  #4  
Nov 15th, 2004
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.
Reply With Quote  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Question about join of thread.

  #5  
Nov 15th, 2004
Thanks jwenting,


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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Question about join of thread.

  #6  
Nov 15th, 2004
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.
Reply With Quote  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Question about join of thread.

  #7  
Nov 16th, 2004
Thanks jwenting,


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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Question about join of thread.

  #8  
Nov 16th, 2004
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.
Reply With Quote  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Question about join of thread.

  #9  
Nov 17th, 2004
Thanks jwenting,


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
Reply With Quote  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: Question about join of thread.

  #10  
Nov 17th, 2004
Thanks jwenting,


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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 1:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC