JAVA基礎知識之多線程——控制線程

來源:互聯網
上載者:User

標籤:優先順序   運行   使用   set   擷取   for   strong   port   tar   

join線程

在某個線程中調用其他線程的join()方法,就會使當前線程進入阻塞狀態,直到被join線程執行完為止。join方法類似於wait, 通常會在主線程中調用別的線程的join方法,這樣可以保證在所有的子線程執行結束之後在主線程中完成一些統一的步驟。
下面是一個例子,

 

package threads;public class JoinThread extends Thread {public JoinThread(String name) {super(name);}public void run() {for(int i=0; i<20; i++) {System.out.println(getName()+" "+i);}}public static void main(String[] args) throws InterruptedException {new JoinThread("new Thread-1").start();for (int i=0; i<50; i++) {if(i==20){JoinThread jt = new JoinThread("new thread-2");jt.start();//main線程調用了jt線程的join方法,main線程//必須等jt執行完之後才能繼續執行jt.join();}System.out.println(Thread.currentThread().getName()+" "+i);}}}

執行結果,可見當主線程中i=20時,主線程被阻塞了,測試thread-1和thread-2正在執行,一直到thread-2執行結束之後,主線程才繼續執行

main 0new Thread-1 0main 1new Thread-1 1main 2new Thread-1 2main 3new Thread-1 3main 4new Thread-1 4main 5new Thread-1 5main 6new Thread-1 6main 7new Thread-1 7main 8new Thread-1 8main 9new Thread-1 9main 10new Thread-1 10main 11new Thread-1 11main 12new Thread-1 12main 13new Thread-1 13main 14new Thread-1 14main 15new Thread-1 15main 16new Thread-1 16main 17new Thread-1 17main 18new Thread-1 18main 19new Thread-1 19new thread-2 0new thread-2 1new thread-2 2new thread-2 3new thread-2 4new thread-2 5new thread-2 6new thread-2 7new thread-2 8new thread-2 9new thread-2 10new thread-2 11new thread-2 12new thread-2 13new thread-2 14new thread-2 15new thread-2 16new thread-2 17new thread-2 18new thread-2 19main 20main 21main 22main 23main 24main 25main 26main 27main 28main 29main 30main 31main 32main 33main 34main 35main 36main 37main 38main 39main 40main 41main 42main 43main 44main 45main 46main 47main 48main 49
後台線程:setDaemon

後台線程有個特徵就是,如果所有前台線程都死亡,後台線程會自動死亡。JVM的記憶體回收行程就是一種典型的後台線程。Thread提供了一個isDaemon方法判斷是否為後台線程。

下面是一個例子,注意必須線上程start之前設定(setDaemon(true))為後台線程。

package threads;public class DaemonThread extends Thread {public void run() {for(int i=0; i<1000; i++) {System.out.println(getName()+" "+i);}}public static void main(String[] args) {DaemonThread t = new DaemonThread();//必須在start之前設定成後台線程t.setDaemon(true);t.start();for (int i=0; i<3; i++) {System.out.println(Thread.currentThread().getName()+" "+i);}//main進程運行到此處時結束,後台進程也會隨之結束}}

執行結果,可以看到子線程的i並沒有累加到1000就結束了,因為子線程被設定成了後台線程,主線程先於子線程結束,子線程也就隨之結束了

main 0Thread-5 0main 1Thread-5 1main 2Thread-5 2Thread-5 3Thread-5 4...Thread-5 199Thread-5 200Thread-5 201Thread-5 202
線程睡眠:sleep

sleep數Thread類的一個靜態方法,讓當前線程進入阻塞狀態。

在sleep指定的時間到達之前,線程都不會獲得執行機會,即使有可用的CPU執行片,因此sleep方法常常用來暫停線程。

sleep方法需要拋出一個異常。

下面是一個例子,

package threads;import java.util.Date;public class SleepThread {public static void main(String[] args) throws InterruptedException {for (int i=0; i<10; i++) {System.out.println("time:" + new Date());Thread.sleep(1000);}}}

執行結果,

time:Wed Nov 16 12:00:58 CST 2016time:Wed Nov 16 12:00:59 CST 2016time:Wed Nov 16 12:01:00 CST 2016time:Wed Nov 16 12:01:01 CST 2016time:Wed Nov 16 12:01:02 CST 2016time:Wed Nov 16 12:01:03 CST 2016time:Wed Nov 16 12:01:04 CST 2016time:Wed Nov 16 12:01:05 CST 2016time:Wed Nov 16 12:01:06 CST 2016time:Wed Nov 16 12:01:07 CST 2016
線程優先順序 及 線程讓步:yield

優先順序

JAVA 使用setPriority改變線程優先順序。JVM提供了三個常量可在不同平台使用,分別是MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY,當然也可以使用1。。10之類的數字,但是不同平台定義的數字不同,所以還是推薦用常量。

yield

yield也是Thread類的一個靜態方法,它和sleep有些類似,都可以暫停當前線程,不同的是它不會讓線程進入阻塞狀態,而是直接進入就緒狀態,讓當前線程重新進入資源競爭中,此時只有優先順序等於或者高於當前線程的其他線程才能擷取CPU資源執行,否則被yield的線程將重新擷取資源繼續執行。

下面是一個例子,

package threads;public class YieldThread extends Thread{public YieldThread(String name) {super(name);}public void run() {for ( int i = 0; i<50; i++) {System.out.println(getName()+" "+i);if (i == 20) {Thread.yield();}}}public static void main(String[] args) throws InterruptedException {YieldThread yt1 = new YieldThread("thread-1");yt1.setPriority(Thread.MAX_PRIORITY);//yt1.setPriority(7);yt1.start();Thread.sleep(1);YieldThread yt2 = new YieldThread("thead-2  ");yt2.setPriority(Thread.MIN_PRIORITY);//yt2.setPriority(1);yt2.start();}}

執行結果,可以看到,因為thread-1優先順序比thread-2高,所以無輪是thread-1調用了yield還是thread-2調用了yield,都是thread-1先搶到資源繼續執行

thread-1 0thread-1 1thread-1 2thread-1 3thread-1 4thread-1 5thread-1 6thread-1 7thread-1 8thread-1 9thread-1 10thread-1 11thread-1 12thread-1 13thread-1 14thread-1 15thread-1 16thread-1 17thread-1 18thread-1 19thread-1 20thread-1 21thread-1 22thread-1 23thead-2   0thread-1 24thead-2   1thread-1 25thead-2   2thread-1 26thead-2   3thread-1 27thead-2   4thread-1 28thead-2   5thread-1 29thead-2   6thread-1 30thead-2   7thread-1 31thead-2   8thread-1 32thead-2   9thread-1 33thead-2   10thread-1 34thead-2   11thread-1 35thead-2   12thread-1 36thead-2   13thread-1 37thead-2   14thread-1 38thead-2   15thread-1 39thead-2   16thread-1 40thead-2   17thread-1 41thead-2   18thread-1 42thead-2   19thread-1 43thead-2   20thread-1 44thead-2   21thead-2   22thread-1 45thead-2   23thread-1 46thead-2   24thead-2   25thread-1 47thead-2   26thread-1 48thead-2   27thread-1 49thead-2   28thead-2   29thead-2   30thead-2   31thead-2   32thead-2   33thead-2   34thead-2   35thead-2   36thead-2   37thead-2   38thead-2   39thead-2   40thead-2   41thead-2   42thead-2   43thead-2   44thead-2   45thead-2   46thead-2   47thead-2   48thead-2   49

  

 

  

 

JAVA基礎知識之多線程——控制線程

聯繫我們

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