首页 > 编程语言 > 详细

线程优先级_代码演示

时间:2020-04-28 18:10:12      阅读:49      评论:0      收藏:0      [点我收藏+]
package com.qiliang.demo11_线程优先级;

public class ThreadPriorityTest {
    public static void main(String[] args) {
        // 主线程默认优先级
        System.out.println(Thread.currentThread().getName()
                +"线程的优先级为-->"+Thread.currentThread().getPriority());

        MyPriority myPriority = new MyPriority();
        Thread t1 = new Thread(myPriority,"t1");
        Thread t2 = new Thread(myPriority,"t2");
        Thread t3 = new Thread(myPriority,"t3");
        Thread t4 = new Thread(myPriority,"t4");
        Thread t5 = new Thread(myPriority,"t5");
        Thread t6 = new Thread(myPriority,"t6");

        // 先设置优先级,再启动(注意点)
        t1.start();

        t2.setPriority(1);
        t2.start();

        t3.setPriority(4);
        t3.start();

        t4.setPriority(Thread.MAX_PRIORITY); // 10
        t4.start();

        t5.setPriority(8);
        t5.start();

        t6.setPriority(7);
        t6.start();
        // 错误案例,会报错
        /*t5.setPriority(-1);
        t5.start();

        t6.setPriority(11);
        t6.start();*/

    }
}


class MyPriority implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()
        +"线程的优先级为-->"+Thread.currentThread().getPriority());
    }
}
/*
main线程的优先级为-->5
t1线程的优先级为-->5
t4线程的优先级为-->10
t3线程的优先级为-->4
t5线程的优先级为-->8
t6线程的优先级为-->7
t2线程的优先级为-->1

 */

线程优先级_代码演示

原文:https://www.cnblogs.com/liqiliang1437/p/12795563.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!