• Java多线程-线程的优先级(priority)以及守护线程(daemon)


    前言

    线程的优先级(priority) :是一个常量

    Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定按照优先级决定应该调度哪个线程来执行

    线程的优先级用数字表示,范围从1-10,线程优先级高给的资源会多一些, 线程优先级高并不一定先执行,但是权重就大了,比如1张彩票和10张彩票,中奖率就会提高

    Thread.min_priority=1;
    Thread.max_priority=10;
    Thread.norm_priority=5;
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    获取优先级

    使用以下方式改变或者获取优先级

    getPriority().setPriority(int xxx )
    
    • 1

    注意

    1 不可以将优先级设置负数或者大于10的数 只能1-10 不然会报错

    2 要先设置优先级再启动

    3 代码中如果哪行代码比较重要,我们就把它的优先级提升

    4 优先级高并不是每次都会优先执行,只是权重大了些,更加容易先执行,具体要看cpu的调度

    5 . 优先级低只是意味着调度的概率低,并不是优先级低就不会被调用了,同理,优先级低并不是每次都会优先执行,而是优先执行概率高,这都是看CPU的调度

    
    package com.wyh.thread;
    
    /**
     * @program: Thread
     * @description: 测试线程优先级
     * @author: 魏一鹤
     * @createDate: 2022-01-05 22:08
     **/
    
    public class TestThreadPriority{
        public static void main(String[] args){
            //主线程默认优先级
            System.out.println(Thread.currentThread().getName()+"------------>"+Thread.currentThread().getPriority());
            MyThreadPriority myThreadPriority = new MyThreadPriority();
            Thread thread1 = new Thread(myThreadPriority);
            Thread thread2 = new Thread(myThreadPriority);
            Thread thread3 = new Thread(myThreadPriority);
            Thread thread4 = new Thread(myThreadPriority);
            Thread thread5 = new Thread(myThreadPriority);
            Thread thread6 = new Thread(myThreadPriority);
            Thread thread7 = new Thread(myThreadPriority);
            Thread thread8 = new Thread(myThreadPriority);
            //先设置优先级再启动
    
            ///t1直接启动
            thread1.start();
    
            //t2设置优先级为1再启动
            thread2.setPriority(1);
            thread2.start();
    
            //t3设置优先级为4再启动
            thread3.setPriority(4);
            thread3.start();
    
            //t3设置优先级为默认最高级(10)再启动  MAX_PRIORITY默认最高级10
            thread4.setPriority(Thread.MAX_PRIORITY);
            thread4.start();
    
            //t7设置优先级为默认最低级(1)再启动  MIN_PRIORITY最高级1
            thread7.setPriority(Thread.MIN_PRIORITY);
            thread7.start();
    
            //t8设置优先级为默认中级(5)再启动  NORM_PRIORITY默认最高级5
            thread8.setPriority(Thread.NORM_PRIORITY);
            thread8.start();
    
    
            //t5设置优先级为-1  优先级不能设置为负数 不然会报错
            thread5.setPriority(-1);
            thread5.start();
    
            //t6设置优先级为11  优先级不能超过10 不然会报错
            thread6.setPriority(11);
            thread6.start();
        }
    }
    
    class MyThreadPriority implements  Runnable {
    
        @Override
        public void run() {
            //Thread.currentThread().getPriority() 线程的优先级
            System.out.println(Thread.currentThread().getName()+"------------>"+Thread.currentThread().getPriority());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    守护线程(daemon)

    有一个方法setDaemon(Boolean xxx),参数为布尔类型,为真守护,为假不守,默认为false,表示是用户线程,正常的线程都是用户线程,除非加了setDaemon(Boolean xxx)才是守护线程

    线程分为用户线程和守护线程

    虚拟机必须确保用户线程执行完毕(main就是一个用户线程)

    虚拟机不用等待守护线程执行完毕(gc垃圾回收就是一个守护线程,后台记录操作日志,监控内存,垃圾回收等待都是守护线程的例子)

    例子

    package com.wyh.thread;
    
    /**
     * @program: Thread
     * @description: 测试线程守护
     * @author: 魏一鹤
     * @createDate: 2022-01-06 22:03
     **/
    
    //可以想象例子 上帝守护人类  在
    // 这个例子中 上帝是永生的 人类是暂时的
    // 上帝相当于守护线程,人类是一个用户线程,上帝守护者我们
    public class TestThreadDaemon {
        public static void main(String[] args){
            //用户
            Person person = new Person();
            Thread threadPerson = new Thread(person);
            //守护
            God god = new God();
            Thread threadGod = new Thread(god);
            //设置守护  默认为false,表示是用户线程
            //正常的线程都是用户线程,除非加了setDaemon(Boolean xxx)才是守护线程
            threadGod.setDaemon(true);
            //上帝守护线程启动
            threadGod.start();
            //人类线程启动
            threadPerson.start();
        }
    
    }
    //上帝
    class  God implements  Runnable {
    
        @Override
        public void run() {
            while (true) {
                System.out.println("上帝守护者人类");
            }
        }
    }
    //人类
    class Person implements  Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 365; i++) {
                System.out.println("人类开开心心的活着"+i+"天");
            }
            System.out.println("=======goodbye======");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    Panda3d 外部硬件接口介绍
    2009奥巴马的秋季开学演讲稿
    两个json字符串可以直接用equals比较是否相等吗
    零基础学python之列表
    Oracle数据库更改账号密码,设置账号有效期
    CountDownLatch与CyclicBarrier
    Jenkins发布失败记录
    Kruskal,346. 走廊泼水节
    【数据分享】2022年我国30米分辨率的山体阴影数据(免费获取)
    Oracle Primavera P6EPPM Mobile/App 安卓移动端分享(长期更新)
  • 原文地址:https://blog.csdn.net/weixin_46713508/article/details/126755896