- Java多線程編程核心技術
- 高洪巖
- 574字
- 2019-01-01 01:10:25
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
推薦閱讀
- Monkey Game Development:Beginner's Guide
- Learning Data Mining with Python
- Java性能權威指南(第2版)
- Responsive Web Design by Example
- Learn React with TypeScript 3
- Visual FoxPro程序設計
- 0 bug:C/C++商用工程之道
- SQL 經典實例
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- Visual C++開發寶典
- HTML5 WebSocket權威指南
- Python滲透測試編程技術:方法與實踐(第2版)
- CryENGINE Game Programming with C++,C#,and Lua
- Getting Started with the Lazarus IDE
- FORTRAN程序設計權威指南