购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

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所示。

图1-31 程序运行结果

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所在类的对象。 VQXpAxyTEG4CRtKpZxaaQ+I6wAXSUGFoRW3rBXtRFypM1QVe/YIeGkM7LC5VUkmE

点击中间区域
呼出菜单
上一章
目录
下一章
×