本篇文章為大家展示了Java 線程的生命周期有哪些,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
為海棠等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及海棠網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、海棠網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
一 代碼
/** * @Title: ThreadStatus.java * @Description: TODO(演示線程的生命狀態(tài)) */ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.TimeUnit; public class ThreadStatus { private static Lock lock = new ReentrantLock(); public static void main(String[] args) { new Thread(new TimeWaiting(), "TimeWaitingThread").start(); new Thread(new Waiting(), "WaitingThread").start(); // 使用兩個Blocked線程,一個獲取鎖,一個被阻塞 new Thread(new Blocked(), "BThread-1").start(); new Thread(new Blocked(), "BThread-2").start(); new Thread(new Sync(), "SyncThread-1").start(); new Thread(new Sync(), "SyncThread-2").start(); } //該線程不斷地進入隨眠 static class TimeWaiting implements Runnable { public void run() { while (true) { try { TimeUnit.SECONDS.sleep(5); System.out.println("I am TimeWaiting Thread: "+ Thread.currentThread().getName()); } catch (InterruptedException e) { } } } } //該線程在Waiting.class實例上等待 static class Waiting implements Runnable { public void run( ) { while (true) { synchronized (Waiting.class) { try { System.out.println("I am Waiting Thread: "+ Thread.currentThread().getName()); Waiting.class.wait( ); } catch (InterruptedException e) { e.printStackTrace( ); } } } } } //該線程在Blocked.class實例上加鎖后,不會釋放該鎖 static class Blocked implements Runnable { public void run( ) { synchronized (Blocked.class) { while (true) { try { System.out.println("I am Blocked Thread: "+ Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) {} } } } } //該線程用于同步鎖 static class Sync implements Runnable { public void run( ) { lock.lock( ); try { System.out.println("I am Sync Thread: "+ Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { } finally { lock.unlock(); } } } }
二 運行
I am Waiting Thread: WaitingThread
I am Blocked Thread: BThread-1
I am Sync Thread: SyncThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am Sync Thread: SyncThread-2
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
上述內(nèi)容就是Java 線程的生命周期有哪些,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:Java線程的生命周期有哪些
網(wǎng)站網(wǎng)址:http://jinyejixie.com/article38/gcecsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站維護、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)、虛擬主機、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)