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所示。
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所在類的對象。
總結
以上是生活随笔為你收集整理的java thread isalive,《Java多线程编程核心技术(第2版)》 —1.4 isAlive()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抽脂多少钱一次啊?
- 下一篇: 还不信者的下一句是什么呢?