我是靠谱客的博主 自信篮球,这篇文章主要介绍java设置优先级,现在分享给大家,希望可以做个参考。

java设置优先级

java线程的调度并不是通过设置优先级就可以搞定的。


public class ThreadDemo {
    public static void main(String[] args) {
        Runnable myRunnable = new MyThread(); // 创建一个Runnable实现类的对象
        Thread thread1 = new Thread(myRunnable, "A");
        Thread thread2 = new Thread(myRunnable, "B");
        Thread thread3 = new Thread(myRunnable, "C");
        thread1.setPriority(Thread.MIN_PRIORITY);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread3.setPriority(Thread.MAX_PRIORITY);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class MyThread implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName());

        }
    }
}

虽然我们对线程的优先级进行了设置,但可以看见,线程的执行并没有按照优先级的顺序来执行

 

 

最后

以上就是自信篮球最近收集整理的关于java设置优先级的全部内容,更多相关java设置优先级内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部