Multithreading in Java (6)

本文介绍Java中使用isAlive()方法检查线程是否存活及使用join()方法确保主线程最后结束的技术。通过示例演示如何在程序中运用这两种方法,并解释其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Multithreading in Java - Using isAlive() and join()


(Page 6 of 10 )

Typically, the main thread is the last thread to finish in a program. However, there isn’t any guarantee that the main thread won’t finish before a child thread finishes. In the previous example, we told the main method to sleep until the child threads terminate. However, we estimated the time it takes for the child threads to complete processing. If our estimate was too short, a child thread could terminate after the main thread terminates. Therefore, the sleep technique isn’t the best one to use to guarantee that the main thread terminates last.

Programmers use two other techniques to ensure that the main thread is the last thread to terminate. These techniques involve calling the isAlive() method and the join() method. Both of these methods are defined in the Thread class.

The isAlive() method determines whether a thread is still running. If it is, the isAlive() method returns a Boolean true value; otherwise, a Boolean false is returned. You can use the isAlive() method to examine whether a child thread continues to run. The join() method works differently than the isAlive() method. The join() method waits until the child thread terminates and “joins” the main thread. In addition, you can use the join() method to specify the amount of time you want to wait for a child thread to terminate.

The following example illustrates how to use the isAlive() method and the join() method in your program. This example is nearly the same as the previous example. The difference lies in the main() method of the Demo class definition.

After the threads are declared using the constructor of the MyThread class, the isAlive() method is called for each thread. The value returned by the isAlive() method is then displayed on the screen. Next, the join() method is called for each thread. The join() method causes the main thread to wait for all child threads to complete processing before the main thread terminates.

Here is what is displayed on the screen when this program runs:

Thread Status: Alive
Thread 1: true
Thread 2: true
Thread 3: true
Thread 4: true
Threads Joining.
Thread: 1
Thread: 2
Thread: 3
Thread: 4
Terminating thread: 1
Terminating thread: 2
Terminating thread: 3
Terminating thread: 4
Thread Status: Alive
Thread 1: false
Thread 2: false
Thread 3: false
Thread 4: false
Terminating thread: main thread.
class MyThread implements Runnable {
  String tName;
  Thread t;
  MyThread (String threadName) {
    
tName = threadName;
     t = new Thread (this, tName);
     t.start();
  }
  public void run() {
    
try {
         System.out.println("Thread: " + tName );
         Thread.sleep(2000);
    
} catch (InterruptedException e ) {
       System.out.println("Exception: Thread "
               
+ tName + " interrupted");
     }
     System.out.println("Terminating thread: " + tName );
 
}
}
class Demo {
   public static void main (String args []) {
      MyThread thread1 = new MyThread ("1");
      MyThread thread2 = new MyThread ("2");
      MyThread thread3 = new MyThread ("3");
      MyThread thread4 = new MyThread ("4");
      System.out.println("Thread Status: Alive");
      System.out.println("Thread 1: "
               
+ thread1.t.isAlive());
      System.out.println("Thread 2: "
               
+ thread2.t.isAlive());
      System.out.println("Thread 3: "
               
+ thread3.t.isAlive());
      System.out.println("Thread 4: "
               
+ thread4.t.isAlive());
     
try {
         System.out.println("Threads Joining.");
         thread1.t.join();
         thread2.t.join();
         thread3.t.join();
         thread4.t.join();
     
} catch (InterruptedException e) {
        System.out.println(
                 
"Exception: Thread main interrupted.");
      }
      System.out.println("Thread Status: Alive");
      System.out.println("Thread 1: "
               
+ thread1.t.isAlive());
      System.out.println("Thread 2: "
               
+ thread2.t.isAlive());
      System.out.println("Thread 3: "
               
+ thread3.t.isAlive());
      System.out.println("Thread 4: "
               
+ thread4.t.isAlive());
      System.out.println(
                "Terminating thread: main thread."); 
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值