java thread isalive,《Java多线程编程核心技术(第2版)》 —1.4 isAlive()方法

1.4 isAlive()方法

isAlive()方法的功能是判断当前的线程是否存活。

新建项目t7,类文件MyThread.java代码如下:

public class MyThread extends Thread {

@Override

public void run() {

System.out.println("run=" + this.isAlive());

}

}

运行Run.java代码如下:

public class Run {

public static void main(String[] args) {

MyThread mythread = new MyThread();

System.out.println("begin ==" + mythread.isAlive());

mythread.start();

System.out.println("end ==" + mythread.isAlive());

}

}

程序运行结果如图1-31所示。

1581141531294109.png

isAlive()方法的作用是测试线程是否处于活动状态。那什么是活动状态呢?线程已经启动且尚未终止的状态即活动状态。如果线程处于正在运行或准备开始运行的状态,就认为线程是“存活”的。

需要说明的是,对于代码:

System.out.println("end ==" + mythread.isAlive());

虽然其输出的值是true,但此值是不确定的。输出true值是因为mythread线程还未执行完毕,如果代码更改如下:

public static void main(String[] args) throws InterruptedException {

MyThread mythread = new MyThread();

System.out.println("begin ==" + mythread.isAlive());

mythread.start();

Thread.sleep(1000);

System.out.println("end ==" + mythread.isAlive());

}

则代码:

System.out.println("end ==" + mythread.isAlive());

输出的结果为false,因为mythread对象已经在1s之内执行完毕。

需要注意的是,main主线程执行Thread.sleep(1000)方法会使main主线程停止1s,而不是将mythread线程停止1s。

另外,在使用isAlive()方法时,如果将线程对象以构造参数的方式传递给Thread对象进行start()启动,则运行的结果和前面示例是有差异的,造成这种差异的原因是Thread.currentThread()和this的差异,下面测试一下这个实验。

创建测试用的isaliveOtherTest项目,创建CountOperate.java文件,代码如下:

package mythread;

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");

}

@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");

}

}

创建Run.java文件,代码如下:

package test;

import mythread.CountOperate;

public class Run {

public static void main(String[] args) {

CountOperate c = new CountOperate();

Thread t1 = new Thread(c);

System.out.println("main begin t1 isAlive=" + t1.isAlive());

t1.setName("A");

t1.start();

System.out.println("main end t1 isAlive=" + t1.isAlive());

}

}

程序运行结果如下:

CountOperate---begin

Thread.currentThread().getName()=main

Thread.currentThread().isAlive()=true

this.getName()=Thread-0

this.isAlive()=false

CountOperate---end

main begin t1 isAlive=false

main end t1 isAlive=true

run---begin

Thread.currentThread().getName()=A

Thread.currentThread().isAlive()=true

this.getName()=Thread-0

this.isAlive()=false

run---end

注意,关键字this代表this所在类的对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值