[轉載] java多線程總結(二)

來源:互聯網
上載者:User

標籤:毫秒級   xtend   setdeamon   基本   extend   thread   ted   自己   範圍   

轉載自:http://www.cnblogs.com/lwbqqyumidi/p/3817517.html

Windstep

 

四.Java多線程的阻塞狀態與線程式控制制

上文已經提到Java阻塞的幾種具體類型。下面分別看下引起Java線程阻塞的主要方法。

1.join()

join —— 讓一個線程等待另一個線程完成才繼續執行。如A線程線程執行體中調用B線程的join()方法,則A線程被阻塞,知道B線程執行完為止,A才能得以繼續執行。

 1 public class ThreadTest { 2  3     public static void main(String[] args) { 4  5         MyRunnable myRunnable = new MyRunnable(); 6         Thread thread = new Thread(myRunnable); 7  8         for (int i = 0; i < 100; i++) { 9             System.out.println(Thread.currentThread().getName() + " " + i);10             if (i == 30) {11                 thread.start();12                 try {13                     thread.join();    // main線程需要等待thread線程執行完後才能繼續執行14                 } catch (InterruptedException e) {15                     e.printStackTrace();16                 }17             }18         }19     }20 }21 22 class MyRunnable implements Runnable {23 24     @Override25     public void run() {26         for (int i = 0; i < 100; i++) {27             System.out.println(Thread.currentThread().getName() + " " + i);28         }29     }30 }

 

2.sleep()

sleep —— 讓當前的正在執行的線程暫停指定的時間,並進入阻塞狀態。在其睡眠的時間段內,該線程由於不是處於就緒狀態,因此不會得到執行的機會。即使此時系統中沒有任何其他可執行檔線程,出於sleep()中的線程也不會執行。因此sleep()方法常用來暫停線程執行。

前面有講到,當調用了建立的線程的start()方法後,線程進入到就緒狀態,可能會在接下來的某個時間擷取CPU時間片得以執行,如果希望這個新線程必然性的立即執行,直接調用原來線程的sleep(1)即可。

 1 public class ThreadTest { 2  3     public static void main(String[] args) { 4  5         MyRunnable myRunnable = new MyRunnable(); 6         Thread thread = new Thread(myRunnable); 7  8         for (int i = 0; i < 100; i++) { 9             System.out.println(Thread.currentThread().getName() + " " + i);10             if (i == 30) {11                 thread.start();12                 try {13                     Thread.sleep(1);   // 使得thread必然能夠馬上得以執行14                 } catch (InterruptedException e) {15                     e.printStackTrace();16                 }17             }18         }19     }20 }21 22 class MyRunnable implements Runnable {23 24     @Override25     public void run() {26         for (int i = 0; i < 100; i++) {27             System.out.println(Thread.currentThread().getName() + " " + i);28         }29     }30 }

註:睡一個毫秒級夠了,因為CPU不會空閑,會切換到建立的線程。

 

3.後台線程(Daemon Thread)

概念/目的:後台線程主要是為其他線程(相對可以稱之為前台線程)提供服務,或“守護線程”。如JVM中的記憶體回收線程。

生命週期:後台線程的生命週期與前台線程生命週期有一定關聯。主要體現在:當所有的前台線程都進入死亡狀態時,後台線程會自動死亡(其實這個也很好理解,因為後台線程存在的目的在於為前台線程服務的,既然所有的前台線程都死亡了,那它自己還留著有什麼用...偉大啊 ! !)。

設定後台線程:調用Thread對象的setDaemon(true)方法可以將指定的線程設定為後台線程。

 1 public class ThreadTest { 2  3     public static void main(String[] args) { 4         Thread myThread = new MyThread(); 5         for (int i = 0; i < 100; i++) { 6             System.out.println("main thread i = " + i); 7             if (i == 20) { 8                 myThread.setDaemon(true); 9                 myThread.start();10             }11         }12     }13 14 }15 16 class MyThread extends Thread {17 18     public void run() {19         for (int i = 0; i < 100; i++) {20             System.out.println("i = " + i);21             try {22                 Thread.sleep(1);23             } catch (InterruptedException e) {24                 // TODO Auto-generated catch block25                 e.printStackTrace();26             }27         }28     }29 }

判斷線程是否是後台線程:調用thread對象的isDeamon()方法。

註:main線程預設是前台線程,前台線程建立中建立的子線程預設是前台線程,後台線程中建立的線程預設是後台線程。調用setDeamon(true)方法將前台線程設定為後台線程時,需要在start()方法調用之前。前天線程都死亡後,JVM通知後台線程死亡,但從接收指令到作出響應,需要一定的時間。

 

4.改變線程的優先順序/setPriority():

每個線程在執行時都具有一定的優先順序,優先順序高的線程具有較多的執行機會。每個線程預設的優先順序都與建立它的線程的優先順序相同。main線程預設具有普通優先順序。

設定線程優先順序:setPriority(int priorityLevel)。參數priorityLevel範圍在1-10之間,常用的有如下三個靜態常量值:

MAX_PRIORITY:10

MIN_PRIORITY:1

NORM_PRIORITY:5

擷取線程優先順序:getPriority()。

註:具有較高線程優先順序的線程對象僅表示此線程具有較多的執行機會,而非優先執行。

 1 public class ThreadTest { 2  3     public static void main(String[] args) { 4         Thread myThread = new MyThread(); 5         for (int i = 0; i < 100; i++) { 6             System.out.println("main thread i = " + i); 7             if (i == 20) { 8                 myThread.setPriority(Thread.MAX_PRIORITY); 9                 myThread.start();10             }11         }12     }13 14 }15 16 class MyThread extends Thread {17 18     public void run() {19         for (int i = 0; i < 100; i++) {20             System.out.println("i = " + i);21         }22     }23 }

 

5.線程讓步:yield()

上一篇博文中已經講到了yield()的基本作用,同時,yield()方法還與線程優先順序有關,當某個線程調用yiled()方法從運行狀態轉換到就緒狀態後,CPU從就緒狀態線程隊列中只會選擇與該線程優先順序相同或優先順序更高的線程去執行。

 1 public class ThreadTest { 2  3     public static void main(String[] args) { 4         Thread myThread1 = new MyThread1(); 5         Thread myThread2 = new MyThread2(); 6         myThread1.setPriority(Thread.MAX_PRIORITY); 7         myThread2.setPriority(Thread.MIN_PRIORITY); 8         for (int i = 0; i < 100; i++) { 9             System.out.println("main thread i = " + i);10             if (i == 20) {11                 myThread1.start();12                 myThread2.start();13                 Thread.yield();14             }15         }16     }17 18 }19 20 class MyThread1 extends Thread {21 22     public void run() {23         for (int i = 0; i < 100; i++) {24             System.out.println("myThread 1 --  i = " + i);25         }26     }27 }28 29 class MyThread2 extends Thread {30 31     public void run() {32         for (int i = 0; i < 100; i++) {33             System.out.println("myThread 2 --  i = " + i);34         }35     }36 }

[轉載] 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.