zoukankan      html  css  js  c++  java
  • 笔记20200521002:多线程【线程的优先级】

    package com.chengguo.线程;
    
    /**
     * 测试线程的优先级
     */
    public class Demo_20200519001_Priority {
        public static void main(String[] args) {
            //主线程默认优先级
            System.out.println(Thread.currentThread().getName() + "线程的默认优先级为:" + Thread.currentThread().getPriority());
    
            //创建对象
            MyPriority myPriority = new MyPriority();
            //创建线程对象
            Thread thread1 = new Thread(myPriority);
            Thread thread2 = new Thread(myPriority);
            Thread thread3 = new Thread(myPriority);
            Thread thread4 = new Thread(myPriority);
            Thread thread5 = new Thread(myPriority);
            Thread thread6 = new Thread(myPriority);
            //先设置优先级,在启动线程
            thread1.setPriority(3);
            thread1.start();
    
            thread2.start();
    
            thread3.setPriority(1);
            thread3.start();
    
            thread4.setPriority(2);
            thread4.start();
    
            thread5.setPriority(Thread.MAX_PRIORITY);//最大优先级为10
            thread5.start();
    
            thread6.setPriority(7);
            thread6.start();
    
        }
    }
    
    class MyPriority implements Runnable {
    
        @Override
        public void run() {
            //获取线程的名字       测试线程的优先级
            System.out.println(Thread.currentThread().getName() + "--》 优先级为:" + Thread.currentThread().getPriority());
        }
    }
    

      

     
  • 相关阅读:
    Web前端笔记和简历模板
    三种 Loading 制作方案
    注册中心之健康检测机制
    HTTPS与加密
    多线程-JUC
    date日期类型
    spring配置文件约束
    Tomcat web.xml 中的listener、 filter、servlet 加载顺序
    java 日志框架总结
    mysql常用命令
  • 原文地址:https://www.cnblogs.com/sadfoo/p/13128681.html
Copyright © 2011-2022 走看看