官术网_书友最值得收藏!

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

圖1-24 運行結果

方法isAlive()的作用是測試線程是否處于活動狀態。什么是活動狀態呢?活動狀態就是線程已經啟動且尚未終止。線程處于正在運行或準備開始運行的狀態,就認為線程是“存活”的。

需要說明一下,如以下代碼:

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

雖然在上面的示例中打印的值是true,但此值是不確定的。打印true值是因為mythread線程還未執行完畢,所以輸出true。如果代碼更改如下:

    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());
    }

則上述代碼運行的結果輸出為false,因為mythread對象已經在1秒之內執行完畢。

另外,在使用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
主站蜘蛛池模板: 元朗区| 瓮安县| 高要市| 布拖县| 股票| 曲周县| 郑州市| 全州县| 贵南县| 报价| 金秀| 沁水县| 秭归县| 桂林市| 桂平市| 远安县| 公主岭市| 泗阳县| 清苑县| 青浦区| 廉江市| 吉安市| 外汇| 新昌县| 慈溪市| 保靖县| 莲花县| 长治县| 合阳县| 汤阴县| 乌鲁木齐县| 木里| 内江市| 灵石县| 博爱县| 亳州市| 金堂县| 阿尔山市| 瓮安县| 神池县| 陆丰市|