Home>

How does one thread know that another thread has ended?The thread class provides methods to answer this question.

There are two ways to determine if a thread has ended.First, you can call isalive () in a thread. This method is defined by thread, and its general form is as follows:

 final boolean isalive ()

If the called thread is still running,The isalive () method returns true, if not it returns false. But isalive () is rarely used,A more common way to wait for the thread to end is to call join (), which is described as follows:

 final void join () throws interruptedexception

This method waits for the called thread to finish.The name comes from the concept of requiring threads to wait until a specified thread participates.The additional form of join () allows defining a maximum time for waiting for the specified thread to end.Here is an improved version of the previous example.Use join () to ensure that the main thread ends last.It also demonstrates the isalive () method.

//using join () to wait for threads to finish.
class newthread implements runnable {
  string name;//name of thread
  thread t;
  newthread (string threadname) {
    name=threadname;
    t=new thread (this, name);
    system.out.println ("new thread:" + t);
    t.start ();//start the thread
  }
  //this is the entry point for thread.
  public void run () {
    try {
      for (int i=5;i>0;i--) {
        system.out.println (name + ":" + i);
        thread.sleep (1000);
      }
    } catch (interruptedexception e) {
      system.out.println (name + "interrupted.");
    }
    system.out.println (name + "exiting.");
  }
}
class demojoin {
  public static void main (string args []) {
    newthread ob1=new newthread ("one");
    newthread ob2=new newthread ("two");
    newthread ob3=new newthread ("three");
    system.out.println ("thread one is alive:" + ob1.t.isalive ());
    system.out.println ("thread two is alive:" + ob2.t.isalive ());
    system.out.println ("thread three is alive:" + ob3.t.isalive ());
    //wait for threads to finish
    try {
      system.out.println ("waiting for threads to finish.");
      ob1.t.join ();
      ob2.t.join ();
      ob3.t.join ();
    } catch (interruptedexception e) {
      system.out.println ("main thread interrupted");
    }
    system.out.println ("thread one is alive:" + ob1.t.isalive ());
    system.out.println ("thread two is alive:" + ob2.t.isalive ());
    system.out.println ("thread three is alive:" + ob3.t.isalive ());
    system.out.println ("main thread exiting.");
  }
}

The program output looks like this:

new thread:thread [one, 5, main]
new thread:thread [two, 5, main]
new thread:thread [three, 5, main]
thread one is alive:true
thread two is alive:true
thread three is alive:true
waiting for threads to finish.
one:5
two:5
three:5
one:4
two:4
three:4
one:3
two:3
three:3
one:2
two:2
three:2
one:1
two:1
three:1
two exiting.
three exiting.
one exiting.
thread one is alive:false
thread two is alive:false
thread three is alive:false
main thread exiting.

As you can see,After calling join (), the thread terminates execution.

  • Previous Explaining the main thread in Java thread programming
  • Next Method for solving garbled parameters of java page URL address transmission parameter