程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java多線程:“基礎篇”10之線程優先級和守護線程

Java多線程:“基礎篇”10之線程優先級和守護線程

編輯:關於JAVA

1. 線程優先級的介紹

java 中的線程優先級的范圍是1~10,默認的優先級是5。“高優先級線程”會優先於 “低優先級線程”執行。

java 中有兩種線程:用戶線程和守護線程。可以通過isDaemon()方法來區別它們:如果返回false, 則說明該線程是“用戶線程”;否則就是“守護線程”。

用戶線程一般用戶執 行用戶級任務,而守護線程也就是“後台線程”,一般用來執行後台任務。需要注意的是: Java虛擬機在“用戶線程”都結束後會後退出。

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

Every thread has a priority. Threads with higher priority are executed in preference to 

threads with lower priority. Each thread may or may not also be marked as a daemon. When 

code running in some thread creates a new Thread object, the new thread has its priority 

initially set equal to the priority of the creating thread, and is a daemon thread if and 

only if the creating thread is a daemon.
    
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which 

typically calls the method named main of some designated class). The Java Virtual Machine 

continues to execute threads until either of the following occurs:
    
The exit method of class Runtime has been called and the security manager has permitted the 

exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the 

run method or by throwing an exception that propagates beyond the run method. 
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits 

when the only threads running are all daemon threads.

大致意思是:

每個線程都有一個優先級。“高優先級線程”會優先於“低優先級線程”執行 。每個線程都可以被標記為一個守護進程或非守護進程。在一些運行的主線程中創建新的子線程時,子線 程的優先級被設置為等於“創建它的主線程的優先級”,當且僅當“創建它的主線程是 守護線程”時“子線程才會是守護線程”。

當Java虛擬機啟動時,通常有一個單一的非守護線程(該線程通過是通過main()方法啟動)。JVM會一 直運行直到下面的任意一個條件發生,JVM就會終止運行:

(01) 調用了exit()方法,並且exit()有權 限被正常執行。

(02) 所有的“非守護線程”都死了(即JVM中僅僅只有“守護線程 ”)。

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

2. 線程優先級的示例

我們先看看優先級的示例

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);
        }
    } 
}; 
    
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 0
t2(10), loop 0
t1(1), loop 1
t2(10), loop 1
t1(1), loop 2
t2(10), loop 2
t1(1), loop 3
t2(10), loop 3
t1(1), loop 4
t2(10), loop 4

結果說明:

(01) 主線程main的優先級是5。

(02) t1的優先級被設為1,而t2的優先級被設為10 。cpu在執行t1和t2的時候,根據時間片輪循調度,所以能夠並發執行。

3. 守護線程的示例

下面是守護線程的示例。

// Demo.java
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) {
        }
    } 
}; 
    
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) {
        }
    } 
}
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 0
t2(isDaemon=true), loop 1
t1(isDaemon=false), loop 0
t2(isDaemon=true), loop 2
t2(isDaemon=true), loop 3
t1(isDaemon=false), loop 1
t2(isDaemon=true), loop 4
t2(isDaemon=true), loop 5
t2(isDaemon=true), loop 6
t1(isDaemon=false), loop 2
t2(isDaemon=true), loop 7
t2(isDaemon=true), loop 8
t2(isDaemon=true), loop 9
t1(isDaemon=false), loop 3
t2(isDaemon=true), loop 10
t2(isDaemon=true), loop 11
t1(isDaemon=false), loop 4
t2(isDaemon=true), loop 12

結果說明:

(01) 主線程main是用戶線程,它創建的子線程t1也是用戶線程。

(02) t2是守護線 程。在“主線程main”和“子線程t1”(它們都是用戶線程)執行完畢,只剩t2這個 守護線程的時候,JVM自動退出。

查看本欄目

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved