笔记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());
    }
}

  

 
posted @ 2020-06-15 00:36  忧桑の民工  阅读(95)  评论(0编辑  收藏  举报