Solved

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

Posted on 2006-06-09
8
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 52
Bot application - advice 3 64
jsp CRUD operations with and without prepared statement also hibernatge 1 31
IBM TS2900 (3572) Tape Autoloader Java? 12 61
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

726 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