1.4 isAlive()方法,1.4isalive方法

來源:互聯網
上載者:User

1.4 isAlive()方法,1.4isalive方法

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

線程代碼:

public class TestThread extends Thread{    @Override    public void run() {        System.out.println("run=" + this.isAlive());    }}

運行代碼:

 

public class Main {    public static void main(String[] args) {        TestThread tt = new TestThread();        System.out.println("Begin == " + tt.isAlive());        tt.start();        //這行代碼的結果是不確定的,列印true表示tt線程還未執行完畢,列印false表示tt線程已經執行完畢。下面會給出結果為false的情況        System.out.println("end == " + tt.isAlive());    }}

 

運行結果:

方法isAlive()是測試線程是否處於活躍狀態的方法,

活躍狀態:線程已經啟動且尚未終止。線程處於正在運行或準備開始啟動並執行狀態,就認為線程是“存活的”。

結果為false的執行代碼:

public class Main {    public static void main(String[] args) {        try {            TestThread tt = new TestThread();            System.out.println("Begin == " + tt.isAlive());            tt.start();            //通過這行代碼使當前線程睡眠,讓tt線程完成並結束            Thread.sleep(1000);            System.out.println("end == " + tt.isAlive());        } catch (Exception e) {            e.printStackTrace();        }    }}    

執行結果:

 

在使用isAlive()方法時,如果將線程對象以構造參數的方式傳遞給Thread對象進行start()啟動時,啟動並執行結果和前面會有差異。產生差異的原因在於:Thread.currentThread()和this的差異。

測試線程代碼:

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

執行代碼:

public class Main {    public static void main(String[] args) {        countOperate co = new countOperate();        Thread t = new Thread(co);        System.out.println("main begin t isAlive = " + t.isAlive());        t.setName("A");        t.start();        System.out.println("main end t isAlive = " + t.isAlive());    }}

執行結果:

 

最後這個運行結果, 真的是驚了。我覺得run裡面的this.isAlive()應該為true的,我認為this指代的就是開啟的另一條線程,而main跟a其實都是主線程。後來發現不太對:

修改執行代碼:

public class Main {    public static void main(String[] args) {        countOperate co = new countOperate();        Thread t = new Thread(co);        System.out.println("main begin t isAlive = " + t.isAlive());        //t.setName("A");        t.start();        System.out.println("main end t isAlive = " + t.isAlive());    }}

執行結果:

然後我思考了一下:在個人的見解上認為了Thread.currentThread()擷取的線程與this的線程的區別。

Thread.currentThread()擷取的為當前啟動並執行線程

    註:(在完成CountOperate的構造方法時,啟動並執行是主線程,在調用start()時啟動並執行是t線程。)。

this擷取的為當前所在的線程

    註:(所以不論是構造方法中還是run方法中他都位於同一個線程,且這個線程從頭到尾都沒有被開啟)。

源碼地址:https://github.com/lilinzhiyu/threadLearning

 

本文內容是書中內容兼具自己的個人看法所成。可能在個人看法上會有諸多問題(畢竟知識量有限,導致認知也有限),如果讀者覺得有問題請大膽提出,我們可以相互交流、相互學習,歡迎你們的到來,心成意足,等待您的評價。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.