Java多線程(十)——線程優先順序和守護線程

來源:互聯網
上載者:User

標籤:com   虛擬機器啟動   優先   false   pre   end   說明   根據   span   

一、線程優先順序的介紹

     java 中的線程優先順序的範圍是1~10,預設的優先順序是5。“高優先順序線程”會優先於“低優先順序線程”執行。

     java 中有兩種線程:使用者線程守護線程。可以通過isDaemon()方法來區別它們:如果返回false,則說明該線程是“使用者線程”;否則就是“守護線程”。使用者線程一般用於執行使用者級任務,而守護線程也就是“後台線程”,一般用來執行背景工作。需要注意的是:Java虛擬機器在“使用者線程”都結束後會後退出。

JDK 中關於線程優先順序和守護線程的介紹如下:

      每個線程都有一個優先順序。“高優先順序線程”會優先於“低優先順序線程”執行。每個線程都可以被標記為一個守護進程或非守護進程。在一些啟動並執行主線程中建立新的子線程時,子線程的優先順序被設定為等於“建立它的主線程的優先順序”,若且唯若“建立它的主線程是守護線程”時“子線程才會是守護線程”。

    當Java虛擬機器啟動時,通常有一個單一的非守護線程(該線程通過是通過main()方法啟動)。JVM會一直運行直到下面的任意一個條件發生,JVM就會終止運行:
    (01) 調用了exit()方法,並且exit()有許可權被正常執行。
    (02) 所有的“非守護線程”都死了(即JVM中僅僅只有“守護線程”)。

    每一個線程都被標記為“守護線程”或“使用者線程”。當只有守護線程運行時,JVM會自動結束。

二、線程優先順序的樣本

我們先看看優先順序的樣本 :

package com.demo.threadPriority;public class MyThread extends Thread{        public MyThread(String name) {        super(name);    }    public void run(){        for (int i=0; i<5; i++) {            System.out.println(Thread.currentThread().getName()                    +"("+Thread.currentThread().getPriority()+ ")"                    +", loop "+i);        }    } }
package com.demo.threadPriority;public class Demo {        public static void main(String[] args) {          System.out.println(Thread.currentThread().getName()                +"("+Thread.currentThread().getPriority()+ ")");        Thread t1=new MyThread("t1");    // 建立t1        Thread t2=new MyThread("t2");    // 建立t2        t1.setPriority(1);               // 設定t1的優先順序為1        t2.setPriority(10);              // 設定t2的優先順序為10        t1.start();                      // 啟動t1        t2.start();                      // 啟動t2    }  }

運行結果:

main(5)t1(1), loop 0t1(1), loop 1t2(10), loop 0t1(1), loop 2t2(10), loop 1t1(1), loop 3t2(10), loop 2t1(1), loop 4t2(10), loop 3t2(10), loop 4

結果說明
(01) 主線程main的優先順序是5。
(02) t1的優先順序被設為1,而t2的優先順序被設為10。cpu在執行t1和t2的時候,根據時間片輪循調度,所以能夠並發執行。

三、守護線程的樣本

下面是守護線程的樣本。

package com.demo.daemonThread;public class MyThread extends Thread{        public MyThread(String name) {        super(name);    }    public void run(){        try {            for (int i=0; i<5; i++) {                Thread.sleep(3);                System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);            }        } catch (InterruptedException e) {        }    } }
package com.demo.daemonThread;public class MyDaemon extends Thread{    public MyDaemon(String name) {        super(name);    }    public void run(){        try {            for (int i=0; i<10000; i++) {                Thread.sleep(1);                System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);            }        } catch (InterruptedException e) {        }    } }
package com.demo.daemonThread;public class Demo {    public static void main(String[] args) {          System.out.println(Thread.currentThread().getName()                +"(isDaemon="+Thread.currentThread().isDaemon()+ ")");        Thread t1=new MyThread("t1");    // 建立t1        Thread t2=new MyDaemon("t2");    // 建立t2        t2.setDaemon(true);              // 設定t2為守護線程        t1.start();                      // 啟動t1        t2.start();                      // 啟動t2    }  }

運行結果:

main(isDaemon=false)t2(isDaemon=true), loop 0t2(isDaemon=true), loop 1t1(isDaemon=false), loop 0t2(isDaemon=true), loop 2t1(isDaemon=false), loop 1t2(isDaemon=true), loop 3t2(isDaemon=true), loop 4t1(isDaemon=false), loop 2t2(isDaemon=true), loop 5t2(isDaemon=true), loop 6t1(isDaemon=false), loop 3t2(isDaemon=true), loop 7t2(isDaemon=true), loop 8t2(isDaemon=true), loop 9t1(isDaemon=false), loop 4

結果說明
(01) 主線程main是使用者線程,它建立的子線程t1也是使用者線程。
(02) t2是守護線程。在“主線程main”和“子線程t1”(它們都是使用者線程)執行完畢,只剩t2這個守護線程的時候,JVM自動結束。

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.