Waiting for the end of multiple threads

A

Andrea B.

In my java application i need to launch several parallel threads to
execute elementary parts of a complex calculation.

So, the main application should wait for the end of all the threads
and evaluate a general result.

I also have to notify to the main application when some thread exceed
a maximum timeout.

Could someone tell me how to implement this programming logic in Java?

I would be grateful if you could send me some sample code.

Thanks in advance
 
N

Nick Pomfret

Andrea B. said:
In my java application i need to launch several parallel threads to
execute elementary parts of a complex calculation.

So, the main application should wait for the end of all the threads
and evaluate a general result.

I also have to notify to the main application when some thread exceed
a maximum timeout.

Could someone tell me how to implement this programming logic in Java?

I would be grateful if you could send me some sample code.

Thanks in advance

Have you tried looking at the Concurrent library
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html?
Its worth a read and will no doubt contain some classes that will help solve
your problems.
 
M

Matt Humphrey

Andrea B. said:
In my java application i need to launch several parallel threads to
execute elementary parts of a complex calculation.

So, the main application should wait for the end of all the threads
and evaluate a general result.

I also have to notify to the main application when some thread exceed
a maximum timeout.

Could someone tell me how to implement this programming logic in Java?

I would be grateful if you could send me some sample code.

Waiting for the end of many threads is very easy as you know you have to
wait at least as long as the longest running thread. Simply join each one
in turn. If the thread has already ended, the join will return immediately.
If the thread has not, it will wait and other threads will be finishing in
the mean time. When you reach the end of the list you will not have waited
much more than the length of the longest thread.

List threads; // Say this list holds all the threads you must wait for
// When waiting for them to end
for (int i = 0; i < threads.size (); ++i) {
((Thread)threads.get(i)).join ();
}

The timeout aspect can be much more complicated, although I'm not sure what
you're asking for because you don't say what the main task will do when it
finds out the thread has exceeded its timeout. That is, does it simply want
to know to update a display, or does it want to stop the thread and possibly
start a new one or does it simply not want to wait more than X seconds for
the entire task to complete? The solutions for these are very different
and some may be simpler if we knew more about what you're trying to do.

From your description, I would guess that what you want is for each thread
to have a potentially different timeout and that you want the main task to
be able to stop an over-running thread and take some corrective action. In
one solution the main task has a monitoring thread that has a list of the
other threads and the expiration time of each one. This thread is set to
sleep until the soonest of the expiring threads is due. When a thread
finishes, it notifies the main task monitoring thread, which removes it from
the list. The monitoring thread finds the next soonest expiration time and
goes back to sleep. If the monitor thread ever wakes up on its own, it
removes the threads that were supposed to expire by invoking a custom stop
command on them, and then goes back to waiting for the next ones until there
are no more threads. Designing and coding this mechanism is not a small job
and requires some skills with Java thread synchronization and wait / notify.

Is that what you're looking for?

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
O

Olivier Merigon

Hi,

This is what I use in my project. I hope it will fit your needs.

/*
* Created on 22 janv. 2004
*/
package com.iratensolutions.tools.common;

/**
* The thread manager class is used to manage the registration of threads.
* It is usefull, for example if you need to wait for a set of threads to
terminate.
* All the threads have to register against the manager. Then when they
terminate,
* they unregister themself. The method
<code>waitForAllThreadToUnregister()</code>
* blocks until all the thread unregister or return if no thread is
registered.
* @author Olivier MERIGON
*/
public class ThreadManager {

/** The thread manager singleton */
private static ThreadManager singleton = new ThreadManager();
/** The number of registered thread */
private int threadCounter;

/**
* Default constructor.
* Create a thread manager with zero thread registered.
*/
public ThreadManager() {
threadCounter = 0;
}

/**
* Register a thread against the thread manager.
*/
public synchronized void registerThread() {
threadCounter++;
}

/**
* Unregister a thread against the thread manager.
*/
public synchronized void unregisterThread() {
threadCounter--;
notify();
}

/**
* Wait for all thread to unregister against the thread manager
*/
public synchronized void waitForAllThreadToUnregister() {
while( threadCounter > 0){
try {
wait();
} catch (InterruptedException e) {
}
}
}

/**
* Wait for a thread to unregister against the thread manager
* @param timeout timeout time in mullisec, -1 if you don't want a timeout
*/
public synchronized void waitAThreadToUnregister(long timeout) {
if (threadCounter <= 0)
return;
try {
if (timeout != -1) {
wait(timeout);
} else {
wait();
}
} catch (InterruptedException e) {
}
}

/**
* Return the thread manager singleton
* @return the default manager.
*/
public static ThreadManager getManager() {
return singleton;
}
}
 
O

Olivier Merigon

Hi again,

This a version of waitForAllThreadToUnregister() with a timeout parameter:

/**
* Wait for all thread to unregister against the thread manager with a
time out
* ABSOLUTLY NOT TESTED
*/
public synchronized void waitForAllThreadToUnregister(long timeout) {
while (threadCounter > 0) {
try {
long time1 = System.currentTimeMillis();
wait(timeout);
long time2 = System.currentTimeMillis();
//All the thread are finished, so we return
if (threadCounter <= 0)
return;
//End of time out, so we return
else if ((time2 - time1) >= timeout)
return;
//else we still need to wait a bit
else
timeout -= time2 - time1;
} catch (InterruptedException e) {
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top