前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线程的优先级和守护线程

线程的优先级和守护线程

作者头像
黑洞代码
发布2021-01-14 15:44:52
1.1K0
发布2021-01-14 15:44:52
举报

线程的优先级和守护线程

概述


  1. 线程的优先级
  2. 线程优先级的特性
  3. 守护线程
  4. 总结

第1节 线程的优先级


  1. 在Java 中,线程优先级的范围是1~10,默认的优先级是5。
  2. “高优先级线程”会优先于“低优先级线程”执行。

第2节 线程优先级的特性


1.线程A启动线程B,线程A和B具有相同的优先级

2.CPU尽量将执行的资源让给优先级高的线程用,但是不一定是优先级较大的线程先执行完。

3.即使线程设有优先级,并不能保证执行先后,线程运行具有随机性。


线程优先级的继承特性

代码语言:javascript
复制
public class MyThread1 extends Thread {
    @Override
    public void run() {
        super.run();
        //输出线程级别
        System.out.println("MyThread1 Priority = " + this.getPriority());
        //启动线程MyThread2
        MyThread2 myThread2 = new MyThread2();
        myThread2.start();
    }
}
代码语言:javascript
复制
public class MyThread2 extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println("MyThread2 Priority = " + this.getPriority());
    }
}
代码语言:javascript
复制
public class ThreadPriority {
    public static void main(String[] args) {
        System.out.println("once Main Thread Priority = " + Thread.currentThread().getPriority());
        //待会打开下面注释再看结果
        Thread.currentThread().setPriority(10);
        System.out.println("twice Main Thread Priority = " + Thread.currentThread().getPriority());
        MyThread1 myThread1 = new MyThread1();
        myThread1.start();
    }
}

执行结果如下:

代码语言:javascript
复制
once Main Thread Priority = 5
twice Main Thread Priority = 10
MyThread1 Priority = 10
MyThread2 Priority = 10

验证线程规则性和随机性

代码语言:javascript
复制
public class MyThread1 extends Thread {
    @Override
    public void run() {
        long start = System.currentTimeMillis();
        System.out.println("------1------ thread 1 start running");
        long count = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                count = count + i;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("------1------ thread 1 use time = " + (end - start));
    }
}
代码语言:javascript
复制
public class MyThread2 extends Thread {
    @Override
    public void run() {
        long start = System.currentTimeMillis();
        System.out.println("------2------ thread 2 start running");
        long count = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                count = count + i;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("------2------ thread 2 use time = " + (end - start));
    }
}
代码语言:javascript
复制
public class ThreadPriority {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            MyThread1 myThread1 = new MyThread1();
            myThread1.setPriority(1);
            MyThread2 myThread2 = new MyThread2();
            myThread2.setPriority(10);
            myThread1.start();
            myThread2.start();
        }
    }
}

执行结果如下:

代码语言:javascript
复制
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 start running
------2------ thread 2 start running
------1------ thread 1 use time = 889
------2------ thread 2 use time = 938
------2------ thread 2 use time = 904
------1------ thread 1 use time = 901
------2------ thread 2 use time = 947
------1------ thread 1 use time = 969
------2------ thread 2 use time = 974
------2------ thread 2 use time = 975
------1------ thread 1 use time = 981
------1------ thread 1 use time = 986
------1------ thread 1 use time = 1011
------2------ thread 2 use time = 1012
------1------ thread 1 use time = 1017
------2------ thread 2 use time = 1035
------2------ thread 2 use time = 1036
------2------ thread 2 use time = 1041
------2------ thread 2 use time = 1042
------1------ thread 1 use time = 1041
------1------ thread 1 use time = 1043
------1------ thread 1 use time = 1043

第3节 守护线程


1.用户线程——执行用户级的任务。

2.守护线程——后台线程,一般用于执行后台任务。

3.isDaemon()方法来区分:如果返回false,则说明该线程是“用户线程”;否则就是“守护线程”。

4.Java虚拟机在“用户线程”都结束后会后退出。

5.守护线程是指在程序运行的时候在后台提供一种通用服务的线程。

6.守护线程并不属于程序中不可或缺的部分。如垃圾回收线程。

7.当所有的用户线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。

8.当所有的用户线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。

9.因为没有了被守护者,守护线程也就没有工作可做了,也就没有继续运行程序的必要了。

10.守护线程并非只有虚拟机内部特有的。

11.Thread.setDaemon()方法可以设置守护线程。

12.如果想设置线程有守护线程,必须在线程运行前设置,否则会抛IllegalThreadStateException异常。

13.守护线程创建的子线程也是守护线程。

第4节 总结


1.线程有优先级之分——优先级从1到10,默认优先级是5。

2.优先级高的线程尽量比优先级低的线程先运行。

3.线程优先级的特性:继承性、规则性、随机性。

4.Java中线程分为2种:用户线程和守护线程。

5.守护线程在JVM中所有用户线程都结束后退出。

6.用户可以手动创建守护线程。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 落叶飞翔的蜗牛 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档