Java多线程:“基本篇”10之线程优先级和守护线程
当前位置:以往代写 > JAVA 教程 >Java多线程:“基本篇”10之线程优先级和守护线程
2019-06-14

Java多线程:“基本篇”10之线程优先级和守护线程

Java多线程:“基本篇”10之线程优先级和守护线程

副标题#e#

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的时候,按照时间片轮循调治,所以可以或许并发执行。


#p#副标题#e#

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
    }  
}

#p#分页标题#e#

运行功效:

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自动退出。

查察本栏目

    关键字:

在线提交作业