Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Check how many threads that are running

Thread view: 
Petterson Mikael - 01 Dec 2005 17:05 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.

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    *



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.