博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java~在使用isAlive时, 将线程对象已构造参数的形式传递给Thread对象时进行start启动时, 使用this和Thread.currentThread的差异
阅读量:4050 次
发布时间:2019-05-25

本文共 3781 字,大约阅读时间需要 12 分钟。

文章目录

currentThread()方法

  • 该方法可返回代码段正在被哪个线程调用的信息
  • 代码示例
/** * Created with IntelliJ IDEA. * Description: If you don't work hard, you will a loser. * User: Listen-Y. * Date: 2020-09-28 * Time: 21:54 */public class CurrentThreadTest extends Thread{
public CurrentThreadTest() {
System.out.println("构造方法name:" + Thread.currentThread().getName()); } @Override public void run() {
System.out.println("run方法name:" + Thread.currentThread().getName()); }}
/** * Created with IntelliJ IDEA. * Description: If you don't work hard, you will a loser. * User: Listen-Y. * Date: 2020-09-28 * Time: 21:55 */public class Run {
public static void main(String[] args) {
CurrentThreadTest threadTest = new CurrentThreadTest(); System.out.println("main方法name:" + Thread.currentThread().getName()); threadTest.start(); }}

在这里插入图片描述

  • 在构造方法中输出的依旧是mian 但是在run方法输出的就是thread-0

isAlive()方法

  • 判断当前线程是否处于活动状态
  • 代码示例
public class IsAliveTest extends Thread {
@Override public void run() {
System.out.println("run=" + this.isAlive()); }}
public class Run2 {
public static void main(String[] args) throws InterruptedException {
IsAliveTest test = new IsAliveTest(); System.out.println("begin=" + test.isAlive()); test.start(); Thread.sleep(100); System.out.println("end=" + test.isAlive()); }}

在这里插入图片描述

使用this和Thread.currentThread的差异

  • 代码
/** * Created with IntelliJ IDEA. * Description: If you don't work hard, you will a loser. * User: Listen-Y. * Date: 2020-09-28 * Time: 22:07 */public class CountOperate extends Thread {
public CountOperate() {
System.out.println("CountOperate构造方法Begin"); System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName()); System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive()); System.out.println("this.getName()=" + this.getName()); System.out.println("this.isAlive()=" + this.isAlive()); System.out.println("CountOperate构造方法End"); System.out.println(); } @Override public void run() {
System.out.println("Run方法Begin"); System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName()); System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive()); System.out.println("this.getName()=" + this.getName()); System.out.println("this.isAlive()=" + this.isAlive()); System.out.println("Run方法End"); System.out.println(); }}
/** * Created with IntelliJ IDEA. * Description: If you don't work hard, you will a loser. * User: Listen-Y. * Date: 2020-09-28 * Time: 22:13 */public class Run3 {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate(); Thread thread = new Thread(countOperate); System.out.println("main begin thread isAlive=" + thread.isAlive()); thread.setName("AAA"); thread.start(); System.out.println("main end thread isAlive=" + thread.isAlive()); }}
CountOperate构造方法BeginThread.currentThread().getName()=mainThread.currentThread().isAlive()=truethis.getName()=Thread-0this.isAlive=()falseCountOperate构造方法Endmain begin thread isAlive=falsemain end thread isAlive=trueRun方法BeginThread.currentThread().getName()=AAAThread.currentThread().isAlive()=truethis.getName()=Thread-0this.isAlive()=falseRun方法EndProcess finished with exit code 0

分析问题就是我们将线程对象已构造参数的形式传递给Thread对象时进行start启动时, 使用Thread.currentThread()和this或者的信息是不一样的, 这也证实我们CurrentThread方法的使用, 因为我们是将一个线程对象传递给一个线程thread, 那我们启动的时候启动的是thread这个线程, 执行的是被传递的这个线程的run方法, 所以我们在这个对象中使用this的时候, 获得的是这个没有被启动的线程对象的信息, 使用Thread.currentThread的时候, 获得的是调用这个代码段的线程的信息, 也就是thread的信息

转载地址:http://ijsci.baihongyu.com/

你可能感兴趣的文章
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>