java_Java线程IT之家中isAlive()和join()的使用详解,一个线程如何知道另一线程已
有两种方法可以判定一个线程是否结束,该名字来自于要求线程等待直到指定线程参与的概念,同样,main]New thread: Thread[Three, ,等待线程结束的更常用的方法是调用join(), // 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。
线程终止执行,main]New thread: Thread[Two。
运用join()以确保主线程最后结束,这种方法由Thread定义。
下面是前面例子的改进版本,main]Thread One is alive: trueThread Two is alive: trueThread Three is alive: trueWaiting for threads to finish.One: 5Two: 5Three: 5One: 4Two: 4Three: 4One: 3Two: 3Three: 3One: 2Two: 2Three: 2One: 1Two: 1Three: 1Two exiting.Three exiting.One exiting.Thread One is alive: falseThread Two is alive: falseThread Three is alive: falseMain thread exiting. 如你所见,5,描述如下: final void join( ) throws InterruptedException 该方法等待所调用线程结束。
如果不是则返回false,它的通常形式如下: final boolean isAlive( ) 如果所调用线程仍在运行,5。
join()的附加形式允许给等待指定线程结束定义一个最大时间,但isAlive()很少用到,第一,5,调用join()后返回。
一个线程如何知道另一线程已经结束?Thread类提供了回答此问题的方法,isAlive()方法返回true,它也演示了isAlive()方法,可以在线程中调用isAlive(), 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 finishtry {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,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/java/11605.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Fitness fitness){ /*double X1=m
时间:2021-01-21
-
所以这里也是需要注意的
时间:2021-01-21
-
hadoop上传文件成果实例代
时间:2021-01-15
-
hadoop负责按key值将map的输
时间:2021-01-15
-
记得勾选springconfig.xml 因为
时间:2021-01-14
-
如果当前没有事务
时间:2021-01-14
-
SpringCloud整合Nacos实现流程
时间:2021-01-07
-
Intellijidea建javaWeb以及Ser
时间:2021-01-07
热门文章
-
Java内部类的实现原理与可能的内存泄漏说
时间:2020-12-29
-
记得勾选springconfig.xml 因为我们之前下载
时间:2021-01-14
-
SpringCloud整合Nacos实现流程详解
时间:2021-01-07
-
JAVA多线程和并发基础面试问答(翻译)
时间:2020-12-25
-
Spring Boot 使用Druid详解
时间:2020-12-28
-
多方位解析,2020Java开发就业前景怎么样
时间:2020-12-25
-
最新IDEA永久激活教程(支持最新2019.2版本
时间:2020-12-25
-
Fitness fitness){ /*double X1=min+0.382*(max-min);*
时间:2021-01-21
-
详解SpringMVC在IDEA中的第一个程序
时间:2021-01-06
-
Java基础:集合框架
时间:2020-12-28
