java基礎:執行緒方法之設定優先順序

十五樓亮哥發表於2015-02-03
一:看程式

public class MyThread1 extends Thread{
	MyThread1(String name){
		super(name);
	}
	
	@Override
	public void run() {
		for(int i = 0; i < 10000 ; i++){
			System.out.println(getName()+": "+i);
		}
	}

}


public class MyThread2 extends Thread{
	MyThread2(String name){
		super(name);
	}
	
	@Override
	public void run() {
		for(int i = 0; i < 10000 ; i++){
			System.out.println(getName()+": "+i);
		}
	}

}


public class Test {
	public static void main(String[] args) {
		MyThread1 t1 = new MyThread1("t1");
		
		MyThread1 t2 = new MyThread1("t2");
		t1.setPriority(Thread.NORM_PRIORITY + 3);
		t1.start();
		t2.start();
	}

}

二:分析

t1.setPriority(Thread.NORM_PRIORITY + 3); 增加執行緒t1的優先順序

執行可以發現,優先輸出執行緒t1。

相關文章