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
Solved

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

Posted on 2006-06-09
8
280 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 20
Adding multiple JVM environments to RedHat 6 7 47
CSV file parsing thru Java 13 34
hashmap order 17 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

840 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