This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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 ?.
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;
if(Thread.activeCount() <= 1)
done = true;
Saravanan Venkat wrote:Hi All,
So for, it works. but is there a better elegant way to do this ?.
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).
Joined: Apr 17, 2010
Excellent. This is exactly I was looking for. Thanks for the reply.