Java线程编程中isAlive()和join()的使用详解
作者:goldensun 时间:2023-10-29 18:08:07
一个线程如何知道另一线程已经结束?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()后返回,线程终止执行。
标签:Java
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot嵌入式Servlet容器与定制化组件超详细讲解
2023-03-31 09:07:05
![](https://img.aspxhome.com/file/2023/5/75795_0s.png)
Android Hilt依赖注入的使用讲解
2023-11-23 02:13:50
阿里面试Nacos配置中心交互模型是push还是pull原理解析
2023-04-09 06:40:54
![](https://img.aspxhome.com/file/2023/2/72352_0s.png)
C#使用文件流FileStream和内存流MemoryStream操作底层字节数组byte[]
2023-09-04 00:30:55
Android通过记住密码功能学习数据存储类SharedPreferences详解及实例
2023-05-21 21:37:49
![](https://img.aspxhome.com/file/2023/1/95011_0s.png)
Java图形用户界面设计(Swing)的介绍
2022-08-23 03:29:37
![](https://img.aspxhome.com/file/2023/4/75654_0s.jpg)
aop的实现原理_动力节点Java学院整理
2022-04-02 16:09:21
C++普通函数指针与成员函数指针实例解析
2022-09-29 10:19:36
深入理解SpringMVC中央调度器DispatcherServlet
2023-03-11 08:54:48
![](https://img.aspxhome.com/file/2023/4/119184_0s.jpg)
Android UI之ImageView实现图片旋转和缩放
2023-08-04 02:53:39
![](https://img.aspxhome.com/file/2023/6/139146_0s.png)
java设计模式--三种工厂模式详解
2023-11-29 09:54:06
![](https://img.aspxhome.com/file/2023/7/60337_0s.png)
Spring Boot整合Redis的完整步骤
2023-06-03 03:21:56
![](https://img.aspxhome.com/file/2023/2/67652_0s.png)
Spring Cloud动态配置刷新RefreshScope使用示例详解
2022-05-23 15:05:32
详解Spark Sql在UDF中如何引用外部数据
2021-08-17 14:51:17
![](https://img.aspxhome.com/file/2023/4/64124_0s.jpg)
C#如何通过probing指定dll寻找文件夹详解
2023-07-22 12:59:37
SpringBoot整合Mybatis的知识点汇总
2023-11-15 21:56:06
android实现文件读写功能
2022-03-10 16:31:23
springboot2.x 接入阿里云市场短信发送的实现
2023-09-20 23:03:57
![](https://img.aspxhome.com/file/2023/3/58593_0s.png)
Java中包装类介绍与其注意事项
2023-03-20 18:26:36
![](https://img.aspxhome.com/file/2023/0/62810_0s.png)
android端微信支付V3版本地签名统一下单详解
2023-05-18 02:37:44