1.4isAlive()方法
方法isAlive()的功能是判断当前线程是否处于活动状态
活动状态是线程已经启动且尚未终止,线程处于正在运行或准备开始运行的状态,就认为线程是存活的。
测试如下
package com.cky.thread; /**
* Created by edison on 2017/11/28.
*/
public class MyThread9 extends Thread {
@Override
public void run() {
super.run();
System.out.println("run="+this.isAlive());
}
}
package com.cky.test; import com.cky.thread.MyThread9; /**
* Created by edison on 2017/11/28.
*/
public class Test14 {
public static void main(String[] args) throws InterruptedException {
MyThread9 th = new MyThread9();
System.out.println("begin="+th.isAlive());
th.start();
// Thread.sleep(1000);
System.out.println("end="+th.isAlive()); }
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test14
begin=false
end=true
run=true Process finished with exit code 0
结果分析:上面刚开始还没有执行start()方法,所以begin为false,当开启线程之后,先打印end,说明此时cpu切换到了主线程,这时执行end,开启的线程还没有执行完毕,所以end为true,最后开启的线程
执行完run函数的代码就结束了。
下面更改代码如下
package com.cky.test; import com.cky.thread.MyThread9; /**
* Created by edison on 2017/11/28.
*/
public class Test14 {
public static void main(String[] args) throws InterruptedException {
MyThread9 th = new MyThread9();
System.out.println("begin="+th.isAlive());
th.start();
Thread.sleep(1000);
System.out.println("end="+th.isAlive()); }
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7535 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test14
begin=false
run=true
end=false Process finished with exit code 0
结果分析:上面执行start方法之后,调用了sleep方法,此时主线程被迫休眠,cpu切换到了子线程,这时执行完了run函数的代码,子线程已经关闭,这时cpu再切换回主线程,执行最后的end方法,但
此刻子线程已经关闭,不是存活状态。说明子线程在1秒内就执行完了。
在使用isAlive方法时,如果将线程对象以构造参数的方式传递给Thread对象进行start()启动,运行的结果是有差异的,造成这个差异的主要原因还是Thread.currentThread()和this的差异
测试代码如下
package com.cky.thread; /**
* Created by edison on 2017/11/28.
*/
public class CountOperate2 extends Thread{
public CountOperate2 () {
System.out.println("count 。。。 begin");
System.out.println("thread name="+ Thread.currentThread().getName());
System.out.println("thread current isalive="+ Thread.currentThread().isAlive());
System.out.println("this.name="+ this.getName());
System.out.println("this.isalive="+ this.isAlive());
System.out.println("count ... end");
} @Override
public void run() {
super.run();
System.out.println("run 。。。 begin");
System.out.println("thread name="+ Thread.currentThread().getName());
System.out.println("thread current isalive="+ Thread.currentThread().isAlive());
System.out.println("this.name="+ this.getName());
System.out.println("this.isalive="+ this.isAlive());
System.out.println("run ... end");
}
}
package com.cky.test; import com.cky.thread.CountOperate2; /**
* Created by edison on 2017/11/28.
*/
public class Test15 {
public static void main(String[] args) {
CountOperate2 cc = new CountOperate2();
Thread t1 = new Thread(cc);
System.out.println("main begin t1 isalive="+t1.isAlive());
t1.start();
System.out.println("main end t1 isalive="+t1.isAlive());
}
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test15
count 。。。 begin
thread name=main
thread current isalive=true
this.name=Thread-0
this.isalive=false
count ... end
main begin t1 isalive=false
main end t1 isalive=true
run 。。。 begin
thread name=Thread-1
thread current isalive=true
this.name=Thread-0
this.isalive=false
run ... end Process finished with exit code 0
结果分析:
首先执行主函数的时候,实例化线程对象会执行构造函数,所以打印的Thread.currentThread().isAlive是true,但是这个这个线程对象还在实例化,并没有执行start()方法,所以this.isAlive为false,后面打印
t1也是false,之后执行了start()方法,子线程开启,cpu还是切换到了main线程,先执行main函数代码,打印t1为true,之后cpu切换到了子线程,执行run函数的代码,这时Thread.currentThread为true,但是this
却打印为false。个人理解为t1还是A还是存活的,但是由于他是由一个线程对象传递进去的,而里面的这个线程对象也就是thread-0已经不再是存活状态。
1.4isAlive()方法的更多相关文章
- java多线程编程核心技术——第一章总结
目录: 1.1进程.多线程的概念,及线程的优点 1.2多线程的使用 1.3currentThread()方法 1.4isAlive()方法 1.5sleep()方法 1.6getId()方法 1.7停 ...
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- 【.net 深呼吸】细说CodeDom(6):方法参数
本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...
- IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法
直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- [C#] C# 基础回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...
- ArcGIS 10.0紧凑型切片读写方法
首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...
- [BOT] 一种android中实现“圆角矩形”的方法
内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...
随机推荐
- 线特征---LineMatching原理(四)
参考文章:An efficient and robust line segment matching approach based on LBD descriptor and pairwise geo ...
- Struts框架的数据封装二之模型驱动方式
Struts2中提供了两类数据封装的方式? * 第二种方式:模型驱动 > 使用模型驱动的方式,也可以把表单中的数据直接封装到一个JavaBean的对象中,并且表单的写法和之前的写法没有区别! & ...
- netcore sqlserver linq contains生成的sql语句不是使用like而是charIndex
在ef中使用linq调用了contains,结果怎么都查不到值,打开sqlserver profiler 发现生成的sql语句不是使用like...而是CharIndex 参考文档:https://s ...
- Vue 进度条 和 图片的动态更改
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- C++中string类
https://blog.csdn.net/sinat_36184075/article/details/54836053 https://blog.csdn.net/fdqw_sph/article ...
- C++中的npos,size_t,size_type
string类提供了6种查找函数,每种函数以不同形式find命名,这些操作全都返回string::size_type类型的值,以下标形式标记查找匹配所发生的位置,或返回一个名为string::npos ...
- dataTables的学习笔记 -- 未开启服务器数据模式
官方网站:http://www.datatables.net/ (1)未开启服务器数据模式(即"bServerSide" : false),数据会从后台直接全部获取,然后在前台全部 ...
- [Robot Framework] Robot Framework用Execute Javascript对XPath表示的元素执行scrollIntoView操作
有些元素需要通过滚动条滚动才能变得可见. 如果这些元素在DOM结构里面存在,可以通过scrollIntoView让其可见,但如果在DOM结构里面不存在那就要通过拖动滚动条让其变的可见. Execute ...
- [RF] 安装好Robot Framework之后怎样让启动的界面后面不带命令行窗口,且图片以机器人显示
安装好Robot Framework之后,通过 C:\Python27\Scripts\ride.py 启动时会带上一个命令行窗口: 怎样让启动的界面后面不带这个命令行窗口,且图片以机器人显示? 方法 ...
- 最佳运动类APP-Keep设计与欣赏
运动类APP是大家手机中必备的一款软件.如果说谁手机里没有任何涉及运动类APP,那只能说真的与时代脱轨了.近些年随着物质生活条件的改善,人们开始越来越重视自己的身体,所以也越来越多的人会进行身体锻炼. ...