java多線程之join與yield

來源:互聯網
上載者:User

轉載請註明出處

http://blog.csdn.net/pony_maggie/article/details/43897971

作者:小馬

 

先說說join的用法, 在某些情況下,如果子線程裡要進行大量的耗時的運算,主線程可能會在子線程執行完之前結束,但是如果主線程又需要用到子線程的處理結果,也就是主線程需要等待子線程執行完成之後再結束,這個時候就要用到join()。

 

看程式碼範例,

class Thread1 extends Thread{public Thread1(){super("[Thread1] Thread");}public void run(){String threadName = Thread.currentThread().getName();System.out.println(threadName + " start.");try {for(int i = 0; i < 5; i++){System.out.println(threadName + " loop at " + i);Thread.sleep(1000);}System.out.println(threadName + " end.");} catch (Exception e) {System.out.println("exception from " + threadName + ".run");}}}class Thread2 extends Thread{Thread1 t1;public Thread2(Thread1 thread1){super("[Thread2] Thread");this.t1 = thread1;}public void run(){String threadName = Thread.currentThread().getName();System.out.println(threadName + " start.");try {t1.join();System.out.println(threadName + " end.");} catch (Exception e) {System.out.println("exception from" + threadName + ".run");}}}public class JoinAndYieldTestDemo {/** * @param args */public static void main(String[] args) {String threadName = Thread.currentThread().getName();System.out.println(threadName + " start.");Thread1 t1 = new Thread1();Thread2 t2 = new Thread2(t1);try {t1.start();Thread.sleep(2000);t2.start();t2.join();} catch (Exception e) {System.out.println("exception from main");}System.out.println(threadName + " end.");}}


 

運行結果:

main start.    //主線程起動 [Thread1] Thread start.  [Thread1] Thread loop at 0  [Thread1] Thread loop at 1  [Thread2] Thread start.     [Thread1] Thread loop at 2  [Thread1] Thread loop at 3  [Thread1] Thread loop at 4  [Thread1] Thread end.  [Thread2] Thread end.      main end!      



從運行結果分析,t2一定在t1之後才結束,main線程則一定在t2結束之後結束,這都要歸功於各自調用的join方法。

再來說說yield,建議先看看前一篇文章,關於線程優先順序,
http://blog.csdn.net/pony_maggie/article/details/43889225

 

理論上虛擬機器和作業系統會讓優先順序高的的線程更多的擷取執行的機會,這個不要與時間片混淆。yield的意思是放手,放棄,一個線程調用yield意味著它要告訴虛擬機器自己樂意讓其它線程佔用自己的位置。這隻是一個暗示,並不保證會產生效果(使當前線程轉到可運行狀態,runnable)。

 

看程式碼範例,

class Producer extends Thread{public void run(){for(int i = 0; i < 5; i++){System.out.println("i am producer: producerd item " + i);Thread.yield();}}}class Consumer extends Thread{public void run(){for(int i = 0; i < 5; i++){System.out.println("i am Consumer: Consumed item " + i);Thread.yield();}}}public class JoinAndYieldTestDemo {/** * @param args */public static void main(String[] args) {Thread producer = new Producer();Thread consumer = new Consumer();producer.setPriority(Thread.MIN_PRIORITY);consumer.setPriority(Thread.MAX_PRIORITY);producer.start();consumer.start();}}



程式建立了名為生產者和消費者的兩個線程,前者最小優先順序,後者最大優先順序。調用yield方法,兩個線程會依次列印,然後將執行機會交給對方,一直這樣進行下去。如果不加yield,結果是先列印完一個,再是另一個。

 

yield的作用一目瞭然,設定成不同的優先順序,也證明了yield不會受優先順序大小的影響。

 


素材來自ImportNew

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.