Hi,
I can call my compute() may times (event from GUI triggers it). How can
I check how many threads that are running at the same time? ( I have to
print that to a panel.
cheers,
//mikael
My code
=======
public void compute(String numerator, String denominator) {
CalcData data = new CalcData();
data.setN(numerator);
data.setD(denominator);
// My Runnable class
Calculation calc = new Calculation(data);
// Create a new thread
Thread calcThread = new Thread(calc, "Calculation");
calcThread.start();
// Wait for the thread to finish but don't wait longer than a
// specified time
long delayMillis = 20000; // since max is 15 seconds delay in
// calculation
try {
calcThread.join(delayMillis);
if (calcThread.isAlive()) {
// Timeout occurred; thread has not finished
} else {
// Finished
}
} catch (InterruptedException e) {
// Thread was interrupted
}
// wait for theard to finnish before we can fireUpdateOccurred.
fireUpdateOccurred(new UpdateEvent(this, calc.data));
}
Matt Humphrey - 01 Dec 2005 17:42 GMT
> Hi,
>
> I can call my compute() may times (event from GUI triggers it). How can I
> check how many threads that are running at the same time? ( I have to
> print that to a panel.
<snipped code>
Whenever you create a new one, increment a counter. When it finishes,
decrement the counter.
Or keep all running Threads in a list.
Or put them in a ThreadGroup.
The main issue is to be sure to remove them from whatever count, list,
group, etc when they exit, even if they exit by throwing uncaught
exceptions.
(If instead of simply being ready to run, by "running at the same time," you
mean simultaneous execution on parallel multiprocessors, that's something
completely different.)
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Ian Shef - 01 Dec 2005 19:30 GMT
Petterson Mikael <mikael.petterson@era.ericsson.se> wrote in news:dmnahb$5as
$1@news.al.sw.ericsson.se:
> Hi,
>
> I can call my compute() may times (event from GUI triggers it). How can
> I check how many threads that are running at the same time? ( I have to
> print that to a panel.
<SNIP>
How about Thread.activeCount() ?
You might have to adjust for the current thread and any other side threads
that you may have started.

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *