1.4 isAlive()方法
方法isAlive()是判斷當(dāng)前線程是否處于活動(dòng)狀態(tài)。
線程代碼:
public class TestThread extends Thread{@Overridepublic void run() {System.out.println("run=" + this.isAlive());} }運(yùn)行代碼:
?
public class Main {public static void main(String[] args) {TestThread tt = new TestThread();System.out.println("Begin == " + tt.isAlive());tt.start();//這行代碼的結(jié)果是不確定的,打印true表示tt線程還未執(zhí)行完畢,打印false表示tt線程已經(jīng)執(zhí)行完畢。下面會(huì)給出結(jié)果為false的情況System.out.println("end == " + tt.isAlive());} }?
運(yùn)行結(jié)果:
方法isAlive()是測(cè)試線程是否處于活躍狀態(tài)的方法,
活躍狀態(tài):線程已經(jīng)啟動(dòng)且尚未終止。線程處于正在運(yùn)行或準(zhǔn)備開(kāi)始運(yùn)行的狀態(tài),就認(rèn)為線程是“存活的”。
結(jié)果為false的執(zhí)行代碼:
public class Main {public static void main(String[] args) {try {TestThread tt = new TestThread();System.out.println("Begin == " + tt.isAlive());tt.start();//通過(guò)這行代碼使當(dāng)前線程睡眠,讓tt線程完成并結(jié)束Thread.sleep(1000);System.out.println("end == " + tt.isAlive());} catch (Exception e) {e.printStackTrace();}} }執(zhí)行結(jié)果:
?
在使用isAlive()方法時(shí),如果將線程對(duì)象以構(gòu)造參數(shù)的方式傳遞給Thread對(duì)象進(jìn)行start()啟動(dòng)時(shí),運(yùn)行的結(jié)果和前面會(huì)有差異。產(chǎn)生差異的原因在于:Thread.currentThread()和this的差異。
測(cè)試線程代碼:
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");}@Overridepublic 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");} }執(zhí)行代碼:
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());} }執(zhí)行結(jié)果:
?
最后這個(gè)運(yùn)行結(jié)果, 真的是驚了。我覺(jué)得run里面的this.isAlive()應(yīng)該為true的,我認(rèn)為this指代的就是開(kāi)啟的另一條線程,而main跟a其實(shí)都是主線程。后來(lái)發(fā)現(xiàn)不太對(duì):
修改執(zhí)行代碼:
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());} }執(zhí)行結(jié)果:
然后我思考了一下:在個(gè)人的見(jiàn)解上認(rèn)為了Thread.currentThread()獲取的線程與this的線程的區(qū)別。
Thread.currentThread()獲取的為當(dāng)前運(yùn)行的線程
注:(在完成CountOperate的構(gòu)造方法時(shí),運(yùn)行的是主線程,在調(diào)用start()時(shí)運(yùn)行的是t線程。)。
this獲取的為當(dāng)前所在的線程
注:(所以不論是構(gòu)造方法中還是run方法中他都位于同一個(gè)線程,且這個(gè)線程從頭到尾都沒(méi)有被開(kāi)啟)。
源碼地址:https://github.com/lilinzhiyu/threadLearning
?
本文內(nèi)容是書(shū)中內(nèi)容兼具自己的個(gè)人看法所成。可能在個(gè)人看法上會(huì)有諸多問(wèn)題(畢竟知識(shí)量有限,導(dǎo)致認(rèn)知也有限),如果讀者覺(jué)得有問(wèn)題請(qǐng)大膽提出,我們可以相互交流、相互學(xué)習(xí),歡迎你們的到來(lái),心成意足,等待您的評(píng)價(jià)。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lilinzhiyu/p/7941121.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的1.4 isAlive()方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 为什么人是根本?
- 下一篇: 技术资料,老吴的博客 很好的 技术