16 线程优先级

package ThreadDemo;

// 线程优先级 1--10  // 默认优先级5
 // 优先级高不代表一定先执行,概率变高而已
public class Test16_Priority {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); // 主线程的优先级

        // 子线程
        Thread t1 = new Thread(new PriorityDemo(), "t1");
        Thread t2 = new Thread(new PriorityDemo(), "t2");
        Thread t3 = new Thread(new PriorityDemo(), "t3");
        Thread t4 = new Thread(new PriorityDemo(), "t4");

        // 先设置优先级,再启动线程
        t1.setPriority(1);
        t1.start();
        t2.setPriority(5);
        t2.start();
        t3.setPriority(Thread.MAX_PRIORITY);  // 10
        t3.start();
        t4.setPriority(11); // 出错
        t4.start();


    }
}

class PriorityDemo implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
    }  // 打印优先级
}
posted @   被占用的小海海  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示