java筆記線程方式1優先順序

來源:互聯網
上載者:User

標籤:東方   run   線程優先順序   方式   .com   擷取   sys   表示   get   

* 我們的線程沒有設定優先權,肯定有預設優先順序。
 * 那麼,預設優先順序是多少呢?
 * 如何擷取線程對象的優先順序?
 *   public final int getPriority():返回線程對象的優先順序
 * 如何設定線程對象的優先順序呢?
 *   public final void setPriority(int newPriority):更改線程的優先順序。
 *
 * 注意:
 *   線程預設優先順序是5。 
 *   線程優先順序的範圍是:1-10。
 *   線程優先順序高僅僅表示線程擷取的 CPU時間片的幾率高,但是要在次數比較多,或者多次啟動並執行時候才能看到比較好的效果。
 *   
 * IllegalArgumentException:非法參數異常。
 * 拋出的異常表明向方法傳遞了一個不合法或不正確的參數。

 1 public class ThreadPriority extends Thread { 2     @Override 3     public void run() { 4         for (int x = 0; x < 100; x++) { 5             System.out.println(getName() + ":" + x); 6         } 7     } 8 } 9 public class ThreadPriorityDemo {10     public static void main(String[] args) {11         ThreadPriority tp1 = new ThreadPriority();12         ThreadPriority tp2 = new ThreadPriority();13         ThreadPriority tp3 = new ThreadPriority();14 15         tp1.setName("東方不敗");16         tp2.setName("嶽不群");17         tp3.setName("林平之");18 19         // 擷取預設優先順序20         // System.out.println(tp1.getPriority());21         // System.out.println(tp2.getPriority());22         // System.out.println(tp3.getPriority());23 24         // 設定線程優先順序25         // tp1.setPriority(100000);26         27         //設定正確的線程優先順序28         tp1.setPriority(10);29         tp2.setPriority(1);30 31         tp1.start();32         tp2.start();33         tp3.start();34     }35 }
View Code

 

java筆記線程方式1優先順序

聯繫我們

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