zoukankan      html  css  js  c++  java
  • 多线程学习笔记(四)线程的优先级

    线程的优先级告诉程序该线程的重要程度有多大。如果有大量线程都被堵塞,都在等候运行,程序会尽可能地先运行优先级的那个线程。 但是,这并不表示优先级较低的线程不会运行。若线程的优先级较低,只不过表示它被准许运行的机会小一些而已。

    线程的优先级设置可以为1-10的任一数值,Thread类中定义了三个线程优先级,分别是:
    MIN_PRIORITY(1)、NORM_PRIORITY(5)、MAX_PRIORITY(10),一般情况下推荐使用这几个常量,不要自行设置数值。

    不同平台,对线程的优先级的支持不同。 编程的时候,不要过度依赖线程优先级,如果你的程序运行是否正确取决于你设置的优先级是否按所设置的优先级运行,那这样的程序不正确

    任务:
    快速处理:设置高的优先级
    慢慢处理:设置低的优先级

    实例:设置线程的优先级

    public class PriorityDome {
        public static void main(String[] args) {
            Thread thread = new Thread(()->{
                while (true){
                    System.out.println(Thread.currentThread().getName());
                }
            },"线程1");
            Thread thread2 = new Thread(()->{
                while (true){
                    System.out.println(Thread.currentThread().getName());
                }
            },"线程2");
            thread.setPriority(Thread.MAX_PRIORITY);//线程优先级 最大为10
            thread2.setPriority(Thread.MIN_PRIORITY);//最小为1
            thread.start();
            thread2.start();
    
        }
    }
  • 相关阅读:
    316. 去除重复字母
    331. 验证二叉树的前序序列化
    225. 用队列实现栈
    197. 上升的温度
    178. 分数排名
    177. 第N高的薪水
    小程序导航
    css3、js动画特效
    背景透明css
    h5新标签IE8不兼容怎么办?
  • 原文地址:https://www.cnblogs.com/huangzhimin/p/11555889.html
Copyright © 2011-2022 走看看