Java 學習筆記之 線程isAlive方法


isAlive方法:

 

 方法isAlive()功能是判斷當前線程是否處於活動狀態。

活動狀態就是線程啟動且尚未終止,比如正在運行或准備開始運行。

public class IsAliveThread extends Thread {
    public IsAliveThread() {
        System.out.println("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("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");

    }
}

public class ThreadRunMain {
    public static void main(String[] args) {
        testIsAliveThread();
    }
    public static void testIsAliveThread(){
        IsAliveThread ist = new IsAliveThread();
        Thread th = new Thread(ist);
        System.out.println("Main begin th isAlive = " + th.isAlive());
        th.start();
        System.out.println("Main end th isAlive = " + th.isAlive());
    }
}

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM