线程入门之优先级priority

时间:2023-03-08 19:26:58
线程入门之优先级priority
package com.thread;
/**
 * 优先级:
 *      Thread.MAX_PRIORITY:最大优先级 10
 *      Thread.MIN_PRIORITY:最小优先级 1
 *      Thread.NORM_PRIORITY:默认优先级 5
 * 说明:设置优先级后,不是优先级高的执行完了才执行优先级低的,
 *      而是优先级高的占用的cpu多,优先级低的占用的cpu少,
 *      优先级低的让优先级高的。
 * @author  95Yang
 */
public class Thread_priority
{
    public static void main(String[] args)
    {
        Thread t1 = new Thread(new Run1());
        Thread t2 = new Thread(new Run2());
        t1.setPriority(Thread.NORM_PRIORITY +3);//设置正常优先级+3
        t1.start();//并行执行
        t2.start();//并行执行

    }
}

class Run1 implements Runnable{
    public void run(){
        for (int i = 1; i <= 200; i++)
        {
            System.out.println("r1:"+i);
        }
    }
}
class Run2 implements Runnable{
    public void run(){
        for (int i = 1; i <= 200; i++)
        {
            System.out.println("r2:---------------"+i);
        }
    }
}