Solved

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

Posted on 2006-06-09
8
268 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now