线程优先级

高优先级的线程更容易再竞争中获胜
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;

测试代码:

/**
 * 
 * @ClassName: PriorityTest
 * @Description: 线程优先级
 * @author: He LongYun
 * @date: 2019年1月31日 下午3:49:16
 */
public class PriorityTest {
	public static class HightPriority extends Thread {
		static int count = 0;

		@Override
		public void run() {
			while (true) {
				count++;
				if (count > 1000000000) {
					System.out.println("HightPriority");
					break;
				}
			}
		}
	}

	public static class LowPriority extends Thread {
		static int count = 0;

		@Override
		public void run() {
			while (true) {
				count++;
				if (count > 1000000000) {
					System.out.println("LowPriority");
					break;
				}
			}
		}
	}

	public static void main(String[] args) {
		HightPriority high = new HightPriority();
		LowPriority low = new LowPriority();
		high.setPriority(Thread.MIN_PRIORITY);
		low.setPriority(Thread.MAX_PRIORITY);
		high.start();
		low.start();
	}
}

线程优先级

相关文章: