欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线程优先级

程序员文章站 2022-06-10 14:31:17
...
/*
 * 线程的优先级1-10
 * 1.MAX_PRIORITY 10
 * 2.MIN_PRIORITY 1
 * 3.默认是 5
 * 优先级高代表执行的概率会高一点,不是必然会先执行。
 * 
 */
public class MyPriority extends Thread {
	@Override
	public void run() {
		System.err.println(Thread.currentThread().getName() + "--" + Thread.currentThread().getPriority());
	}

	public static void main(String[] args) {
		MyPriority mp = new MyPriority();
		Thread t1 = new Thread(mp);
		Thread t2 = new Thread(mp);
		Thread t3 = new Thread(mp);
		Thread t4 = new Thread(mp);
		Thread t5 = new Thread(mp);
		Thread t6 = new Thread(mp);
		t1.setPriority(MAX_PRIORITY);
		t2.setPriority(MAX_PRIORITY);
		t3.setPriority(MAX_PRIORITY);
		t4.setPriority(MIN_PRIORITY);
		t5.setPriority(MIN_PRIORITY);
		t6.setPriority(MIN_PRIORITY);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
	}

}

执行结果:
Thread-1–10
Thread-2–10
Thread-4–1
Thread-3–10
Thread-6–1
Thread-5–1

相关标签: 线程优先级