多线程 线程优先级 (Priority)

package thread02;

/**
 * 线程的优先级 1——10
 * 1、NORM_PRIORITY 5 默认
 * 2、MIN_PRIORITY	1
 * 3、MAX_PRIORITY	10
 * 
 * 优先级不代表绝对的执行顺序(概率)
 * @author 
 *
 */
public class TestPriority {
	public static void main(String[] args) {
		System.out.println(Thread.currentThread().getPriority());
		
		MytPriority mp = new MytPriority();
		
		Thread t1 = new Thread(mp,"a");
		Thread t2 = new Thread(mp,"b");
		Thread t3 = new Thread(mp,"c");
		Thread t4 = new Thread(mp,"d");
		Thread t5 = new Thread(mp,"e");
		Thread t6 = new Thread(mp,"f");
		Thread t7 = new Thread(mp,"g");
		
		//设置优先级在启动前
		t1.setPriority(Thread.MAX_PRIORITY);
		t2.setPriority(Thread.MAX_PRIORITY);
		t3.setPriority(Thread.MAX_PRIORITY);
		t4.setPriority(Thread.MIN_PRIORITY);
		t5.setPriority(Thread.MIN_PRIORITY);
		t6.setPriority(Thread.MIN_PRIORITY);
		t7.setPriority(Thread.MAX_PRIORITY);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
		t7.start();
	}
}

class MytPriority implements Runnable{
	public void run() {
		System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
	}
}

版权声明:本文为qq_44307111原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。