栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA学习线程优先级

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA学习线程优先级

 如果线程没有没有明确优先级的话,系统会默认分配给了一个5的优先级。优先级:从1~10,其他的都是不合法的,优先级越高,代表这个线程先被调用的概率越高,优先级为10的线程,不一定比优先级为5的线程先执行,只是比他先执行的概率高

常用方法:

       setPriority(int )设置优先级,填数字1~10之间;或者用系统自带的

Thread.NORM_PRIORITY(5),Thread.MIN_PRIORITY(1),Thread.MAX_PRIORITY(10)

        getPriority() 获取该线程的优先级


总结:1、线程优先级高的不一定先执行,只是概率高

           2、先设置优先级,在start()

附一个代码

        

public class Demo11 {
    public static void main(String[] args) {
        Test test = new Test();
        Thread thread = new Thread(test);
        Thread thread1 = new Thread(test);
        Thread thread2 = new Thread(test);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread2.start();
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.start();
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread1.start();
    }
}
class Test implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"的优先级为"+Thread.currentThread().getPriority());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/288233.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号