package J20250730; /** * @author WanJl * @version 1.0 * @title demo05_thread_priority * @description * @create 2025/7/30 */ public class Demo05_Thread_Priority { public static void main(String[] args) { Runnable r1=()->{ for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName()+"->"+i); } }; Thread t1=new Thread(r1,"线程1"); Thread t2=new Thread(r1,"线程2"); //获得线程的优先级 System.out.println(t1.getName()+"的优先级是:"+t1.getPriority()); System.out.println(t2.getName()+"的优先级是:"+t2.getPriority()); //设置线程的优先级 t1.setPriority(10); //将线程1设置最高优先级 t2.setPriority(1); //将线程2设置最低优先级 //再次获取线程的优先级 System.out.println(t1.getName()+"的优先级是:"+t1.getPriority()); System.out.println(t2.getName()+"的优先级是:"+t2.getPriority()); //启动线程 t1.start(); t2.start(); } }