Java线程编程中isAlive()和join()的使用详解

 更新时间:2015年09月29日 15:34:13   投稿:goldensun  
这篇文章主要介绍了Java线程编程中isAlive()和join()的使用详解,是Java入门学习中的基础知识,需要的朋友可以参考下

一个线程如何知道另一线程已经结束?Thread类提供了回答此问题的方法。

有两种方法可以判定一个线程是否结束。第一,可以在线程中调用isAlive()。这种方法由Thread定义,它的通常形式如下:

  final boolean isAlive( )

如果所调用线程仍在运行,isAlive()方法返回true,如果不是则返回false。但isAlive()很少用到,等待线程结束的更常用的方法是调用join(),描述如下:

  final void join( ) throws InterruptedException

该方法等待所调用线程结束。该名字来自于要求线程等待直到指定线程参与的概念。join()的附加形式允许给等待指定线程结束定义一个最大时间。下面是前面例子的改进版本。运用join()以确保主线程最后结束。同样,它也演示了isAlive()方法。

// 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.");
  }
}

程序输出如下所示:

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.

如你所见,调用join()后返回,线程终止执行。

相关文章

  • springboot中报错Invalid character found in the request的解决

    springboot中报错Invalid character found in 

    这篇文章主要介绍了springboot中报错Invalid character found in the request的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • spring cloud gateway请求跨域问题解决方案

    spring cloud gateway请求跨域问题解决方案

    这篇文章主要介绍了spring cloud gateway请求跨域问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • Java使用通配符实现增强泛型详解

    Java使用通配符实现增强泛型详解

    泛型是JAVA重要的特性,使用泛型编程,可以使代码复用率提高。本文将利用通配符实现增强泛型,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-08-08
  • JAVA调用Deepseek的api完成基本对话简单代码示例

    JAVA调用Deepseek的api完成基本对话简单代码示例

    这篇文章主要介绍了JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeek API密钥、添加HTTP客户端依赖、创建HTTP请求并使用示例代码来对接DeepSeek API,需要的朋友可以参考下
    2025-02-02
  • SpringBoot统计、监控SQL运行情况的方法详解

    SpringBoot统计、监控SQL运行情况的方法详解

    这篇文章主要给大家介绍了关于SpringBoot统计、监控SQL运行情况的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02
  • java中数组的应用及方法

    java中数组的应用及方法

    本篇文章介绍了,java中数组的应用及方法。需要的朋友参考下
    2013-04-04
  • Java封装数组实现包含、搜索和删除元素操作详解

    Java封装数组实现包含、搜索和删除元素操作详解

    这篇文章主要介绍了Java封装数组实现包含、搜索和删除元素操作,结合实例形式分析了java针对数组元素的查找、删除、判断等相关操作封装与使用技巧,需要的朋友可以参考下
    2020-03-03
  • SpringBoot快速整合Mybatis、MybatisPlus(代码生成器)实现数据库访问功能

    SpringBoot快速整合Mybatis、MybatisPlus(代码生成器)实现数据库访问功能

    这篇文章主要介绍了SpringBoot快速整合Mybatis、MybatisPlus(代码生成器)实现数据库访问功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • java开发中的误区和细节整理

    java开发中的误区和细节整理

    这篇文章给大家整理了关于JAVA开发中的细节以及经常进入的误区整理,希望我们整理的内容能够给大家提供到帮助。
    2018-04-04
  • SpringBoot中的@ConditionalOnMissingBean注解使用详解

    SpringBoot中的@ConditionalOnMissingBean注解使用详解

    这篇文章主要介绍了SpringBoot中的@ConditionalOnMissingBean注解使用详解,@ConditionalOnMissingBean作用在@Bean定义上,也就是说在容器加载它作用的Bean时,检查容器中是否存在目标类型,需要的朋友可以参考下
    2024-01-01

最新评论