This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes using Thread.activeCount to monitor the end of work by the threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "using Thread.activeCount to monitor the end of work by the threads " Watch "using Thread.activeCount to monitor the end of work by the threads " New topic
Author

using Thread.activeCount to monitor the end of work by the threads

Saravanan Venkat
Greenhorn

Joined: Apr 17, 2010
Posts: 5
Hi All,

I have a calculation which is to be performed multiple times ( I will not know in advance on how many times to be performed, as the current flow is such that it checks for the details and starts a thread to do the calculation whenever required ). But, I do want to advance to the next step after 'calculation', only when all the calculations are done. Currently, when the total number of threads active (based on active count) is less than number allowed (I dont want to start alot of threads, so limited to core processors available in the m\c), a new thread is started. And the whole process waits for the threads to complete (again based on ThreadCount). So for, it works. but is there a better elegant way to do this ?.

pseudo code

for-loop-
{
evaluate a collection
if calculation required
{
while(Thread.activeCount() <= numThrdsAllowed(input parameter) )
assign the job to new thread
}
}

//now I want all the thread to get completed, so
public void threadWait()
{
boolean done = false;
while(!done)
{
if(Thread.activeCount() <= 1)
done = true;
}
}



Thanks in advance,

Rgds,
bengu
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Saravanan Venkat wrote:Hi All,
So for, it works. but is there a better elegant way to do this ?.

Hi,

you could use a thread pool:
For example in fixedThreadPool you declare how many threads it should use (maximum),
then submit tasks to execute - how many tasks you only want, could be hundreds or thousands
The pool creates threads and maintains tasks to be executed itself, you don't have to bother with it,
and it uses only up to declared number of threads to run all submitted tasks.

The pseudo code could look like this:




If there are only 5 tasks to run - it creates 5 threads, if there are 100 tasks - it creates 10 threads
and run sequentially all tasks (but only 10 at the same time).
Saravanan Venkat
Greenhorn

Joined: Apr 17, 2010
Posts: 5
Excellent. This is exactly I was looking for. Thanks for the reply.

Rgds,
Saravanan.V
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using Thread.activeCount to monitor the end of work by the threads
 
Similar Threads
Count all threads
Applet Threads
Predict the output and why is it what it is ? - Thread related Q
Timer Question
threading issue