JAVA线程优先级的认识

package cn.breeziness123.zhx;

/**
 * 线程优先级的认识,优先级只影响执行的先后顺序的概率,优先级大的概率会大
 * 三个优先级:
 * MAX_PRIORITY:10
 * MIN_PRIORITY:1
 * NORM_PRIORITY :5
 */
public class ThreadDemo06 {
    public static void main(String[] args){
        ThreadPriority tp = new ThreadPriority();

        Thread th1 = new Thread(tp,"高一");
        Thread th2 = new Thread(tp,"高二");
        Thread th3 = new Thread(tp,"高三");
        Thread th4 = new Thread(tp,"初一");
        Thread th5 = new Thread(tp,"初二");
        Thread th6 = new Thread(tp,"初三");

        th1.setPriority(Thread.MAX_PRIORITY);//设置优先级必须在线程启动前
        th2.setPriority(Thread.MAX_PRIORITY);
        th3.setPriority(Thread.MAX_PRIORITY);
        th4.setPriority(Thread.MIN_PRIORITY);
        th5.setPriority(Thread.MIN_PRIORITY);
        th6.setPriority(Thread.MIN_PRIORITY);

        th4.start();
        th5.start();
        th6.start();
        th1.start();
        th2.start();
        th3.start();


    }
}

class  ThreadPriority implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"----->"+Thread.currentThread().getPriority());
        Thread.yield();//让出调度,重新竞争
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40731414/article/details/86644630