Solved

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

Posted on 2006-06-09
8
276 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 185
session migration servlets 2 36
nextBoolean(double p) for Random class 3 40
tomcat startup error 5 59
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

786 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