Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java thread question-- How to check if threads are dead in a threadGroup

Posted on 2006-06-09
8
Medium Priority
?
299 Views
Last Modified: 2008-02-26
below is a bare bones threaded app. my questions are in the comments. Thanks in advance for your help.
-jbd


import java.util.*;

public class tester {
   
     public static void main (String[] args) {
        new tester().tester1();
     }  
       
       public void tester1 () {
            try{
                new ReadThread();
                new ReadThread();
               
                //----1-----
                //How can I check to make sure that the threads have finished processing.
                //I think I need to use activeCount() for the thread group
                //but I can't seem to figure out how to associate these threads with a threadgroup
                //though, by default they are in main, and I can't seem to get an active count for threadgroup main
               
           } catch (Exception ex) {
                   System.out.println(ex);
                   ex.printStackTrace();
           }
       }
       
       static class ReadThread extends Thread {
           public int thisThreadNum;
                     
           ReadThread(){
                start();
            }

            public void run() {
                //----2-----
                 //There's a bunch of code here. How do I make sure the thread dies after this code executes.
                //the docs say I need to return something to the run method? not exactly sure how to make sense of that
           
            }
       }
}
0
Comment
Question by:jacobbdrew
8 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 16874265
thread.isAlive() going to tell u whether the thread is dead or not
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16874298
hey u need not return anything from run method. unless there is a condition or another thread launched, all the code inside run method will get executed before the thread dies.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16874304
>>//How can I check to make sure that the threads have finished processing.

you can use join method for this.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Expert Comment

by:ksivananth
ID: 16874311
unless there is a need for grouping thread, no need to go for it and in your case it seems not needed.
0
 
LVL 1

Author Comment

by:jacobbdrew
ID: 16874380
OK. Can you show me how I would implement Thread.isAlive(). bascailly, i need something like this, but that works:
...
 public void tester1 () {
            try{
                new ReadThread(); //where this is thread1
                new ReadThread(); //and this is thread2
               
             //can't get the isAlive part to work here....
              if( !Thread1.isAlive() && !Thread2.isAlive() ){
                          //now tell the database the processing is finished (this part i can do)
             }
               
           } catch (Exception ex) {
                   System.out.println(ex);
                   ex.printStackTrace();
           }
       }
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 2000 total points
ID: 16874394
try this,

public void tester1 () {
            try{
                Thread thread1 = new ReadThread(); //where this is thread1
                Thread thread2 = new ReadThread(); //and this is thread2

thread1.join() ;
thread2.join() ;
               
             //can't get the isAlive part to work here....
//              if( !Thread1.isAlive() && !Thread2.isAlive() ){
                          //now tell the database the processing is finished (this part i can do)
//             }
               
           } catch (Exception ex) {
                   System.out.println(ex);
                   ex.printStackTrace();
           }
       }
0
 
LVL 16

Expert Comment

by:imladris
ID: 16874399
The best way to do that is to keep a reference to the threads, and then call join on them:

 public void tester1 () {

            Thread t1,t2;

            try{
                t1=new ReadThread(); //where this is thread1
                t2=new ReadThread(); //and this is thread2
               
                t1.join();
                t2.join();
                          //now tell the database the processing is finished (this part i can do)
               
           } catch (Exception ex) {
                   System.out.println(ex);
                   ex.printStackTrace();
           }
       }

The join method waits for the thread to "join" this one (by finishing its processing).
0
 
LVL 92

Expert Comment

by:objects
ID: 16874419
In java 5 you can use the threads getState() method to determine if it has terminated

http://java.sun.com/docs/books/tutorial/essential/threads/lifecycle.html

problem with isAlive() is that it also returns true before the thread has started.
To use join() code would look like:

public void tester1 () {
            try{
                Thread t1 = new ReadThread(); //where this is thread1
                Thread t2 = new ReadThread(); //and this is thread2
                t1.join();
                t2.join();

               //now tell the database the processing is finished (this part i can do)
               
           } catch (Exception ex) {
                   System.out.println(ex);
                   ex.printStackTrace();
           }
       }

another option would be to have the thred iitself tell the database processing has finished
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question