1. 程式人生 > 實用技巧 >Java執行緒排程

Java執行緒排程

package com.wkcto.chapter07.method;
/**
 * 執行緒優先順序
 * 	1) 優先順序的取值範圍: 1 ~ 10
 * 	2) 所有執行緒預設的優先順序; 5
 * 	3) 優先順序越高, 獲得CPU執行權的機率越大
 * 	4) t1.setPriority( 10 )  設定執行緒優先順序
 * @author 蛙課網
 *
 */
public class Test04 {

	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i = 1; i <= 100; i++){
					System.out.println( Thread.currentThread().getName() + "-->" + i);
				}
			}
		} , "t1");
		t1.setPriority(1); 				//設定優先順序
		t1.start();
		
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i = 1; i <= 100; i++){
					System.out.println( Thread.currentThread().getName() + "-->" + i);
				}
			}
		} , "t2");
		t2.setPriority(10); 		//設定優先順序
		t2.start();
		
		//列印執行緒的優先順序
		System.out.println( "t1 priority: " + t1.getPriority());
		System.out.println( "t2 priority: " + t2.getPriority());
		System.out.println( "main priority: " + Thread.currentThread().getPriority());
		
		//main執行緒
		for(int i = 1; i <= 100; i++){
			System.out.println( Thread.currentThread().getName() + "-->" + i);
		}
	}

}

Java執行緒睡眠

package com.wkcto.chapter07.method;
/**
 * 執行緒睡眠 ( 休眠 )
 * 	 Thread.sleep( 2000 );
 * 		1) 是靜態方法, 通過Thread類名直接呼叫
 * 		2) 睡眠的單位 是毫秒, 1秒 == 1000 毫秒
 * 		3) sleep()有受檢異常需要預處理
 * 		4) sleep()方法所在的執行緒睡眠 
 * @author 蛙課網
 *
 */
public class Test05 {

	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for( int i = 1 ; i<=100; i++){
					System.out.println( Thread.currentThread().getName() + "--> " + i);
					//當 i == 50 時, 執行緒休眠
					if ( i == 50 ) {
						//run()是重寫了Runnable介面中run(),不能宣告丟擲異常,只能捕獲處理
						try {
							Thread.sleep(3000);			//睡眠3秒
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}, "t1");
		t1.start();
		
//		t1 = null;
		//main執行緒
		for( int i = 1 ; i<=100; i++){
			System.out.println( Thread.currentThread().getName() + "-----> " + i);
			//當i==10時, 讓t1執行緒睡眠 
			/*try {
				t1.sleep(3000); 		//雖然是t1呼叫,實際上是main執行緒睡眠 
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
		}
		
		System.out.println( t1.getState() );  		//TIMED_WAITING , t1處於sleep()睡眠 狀態
	}

}

Java執行緒中斷

package com.wkcto.chapter07.method;
/**
 * 執行緒中斷
 * 		t1.interrupt();		中斷t1執行緒
 * 		一般是把處於睡眠 / 等待中的執行緒給喚醒 
 * 
 * @author 蛙課網
 *
 */
public class Test06 {

	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for( int i = 1 ; i<=100; i++){
					System.out.println( Thread.currentThread().getName() + "--> " + i);
					//當 i == 50 時, 執行緒休眠
					if ( i == 50 ) {
						try {
							Thread.sleep(10000);			//睡眠10秒
						} catch (InterruptedException e) {
//							e.printStackTrace();
						}
					}
				}
			}
		}, "t1");
		t1.start();
		
		//main執行緒
		for( int i = 1 ; i<=100; i++){
			System.out.println( Thread.currentThread().getName() + "--> " + i);
		}
		
		//當main執行緒結束 , 把t1執行緒喚醒
		t1.interrupt();  		//中斷t1執行緒的睡眠, 會丟擲中斷異常
		
//		System.out.println( t1.isInterrupted() );
	}

}

package com.wkcto.chapter07.method;
/**
 * 判斷執行緒的中斷狀態
 * 		t1.isInterrupted(), 例項方法判斷執行緒的中斷狀態, 返回true後,不會清除執行緒的中斷標誌
 * 		Thread.interrupted(), 靜態方法判斷執行緒的中斷狀態, 如果返回true表示執行緒被中斷了, 然後會清除執行緒的中斷標誌
 * 				再判斷執行緒的中斷狀態時, 就是false
 * @author 蛙課網
 *
 */
public class Test07 {

	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
//				如果執行緒沒有被中斷,就一直列印字串
				while( ! Thread.currentThread().isInterrupted() ){
					System.out.println( "wkcto");
				}
				System.out.println("11 : " + Thread.currentThread().isInterrupted()); 
			}
		});
		t1.start();
		
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
//				如果執行緒沒有被中斷,就一直列印字串
				while( ! Thread.interrupted() ){
					System.out.println( "bjpowernode");
				}
				System.out.println("22 : " + Thread.currentThread().isInterrupted()); 
			}
		});
		t2.start();
		
		// main執行緒
		for (int i = 1; i <= 50; i++) {
			System.out.println(Thread.currentThread().getName() + "--> " + i);
		}
		
		//main執行緒結束, 就中斷t1執行緒
		t1.interrupt();
		t2.interrupt();
	}

}

Java執行緒讓步

package com.wkcto.chapter07.method;
/**
 * 執行緒讓步
 * 		Thread.yield(); 
 * 		把執行中的執行緒轉換為就緒狀態
 * @author 蛙課網
 *
 */
public class Test08 {

	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= 100; i++) {
					System.out.println(Thread.currentThread().getName() + "--> " + i);
					//當 i的值是10的倍數時, 執行緒讓步
					if ( i % 10 == 0) {
						Thread.yield();  	//轉換為就緒狀態
					}
				}
			}
		} , "t1");
		t1.start();
		
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= 100; i++) {
					System.out.println(Thread.currentThread().getName() + "-====> " + i);
				}
			}
		} , "t2");
		t2.start();
	}

}

Java執行緒合併

package com.wkcto.chapter07.method;
/**
 * 執行緒合併
 * 		t1.join();  	在當前執行緒中加入t1執行緒,當前執行緒轉為等待狀態, 等到t1執行緒執行完畢後,當前執行緒再轉為就緒狀態
 * @author 蛙課網
 *
 */
public class Test09 {

	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= 100; i++) {
					System.out.println(Thread.currentThread().getName() + "--> " + i);
				}
			}
		} , "t1");
		t1.start();
		
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= 100; i++) {
					System.out.println(Thread.currentThread().getName() + "-====> " + i);
					//當i==10時, 把t1執行緒合併進來
					if ( i == 10 ) {
						try {
//							t1.join();   	//現在加入t1執行緒,  當前執行緒轉為等待狀態, 等到t1執行完後,t2當前執行緒再轉為就緒狀態
							t1.join(1000);  //如果當前執行緒等待1000毫秒後, 不管t1執行緒是否結束 ,都會轉為就緒狀態
						} catch (InterruptedException e) {
							e.printStackTrace();
						} 		
					}
				}
			}
		} , "t2");
		t2.start();
	}

}

Java執行緒終止

package com.wkcto.chapter07.method;
/**
 * 終止執行緒
 * 	相辦法讓run()結束 
 * 	可以為執行緒設計一個布林標誌, 在run()方法中定期判斷這個標誌,來決定是否結束 run()
 * @author 蛙課網
 *
 */
public class Test11 {

	public static void main(String[] args) {
		SubThread1 thread1 = new SubThread1();
		thread1.start();
		
		Prime3 prime3 = new Prime3();
		Thread t2 = new Thread(prime3);
		t2.start();
		
		//main執行緒
		for (int i = 1; i <= 50; i++) {
			System.out.println(Thread.currentThread().getName() + "========> " + i);
		}
		//main執行緒結束 , 終止t1執行緒
		thread1.stopping = true;
		prime3.running = false;
	}

}

class SubThread1  extends Thread{
	boolean stopping = false;
	@Override
	public void run() {
		for (int i = 1; i <= 500; i++) {
			if (stopping) {
				return; 			//結束方法的執行
			}
			System.out.println(Thread.currentThread().getName() + "--> " + i);
		}
	}
}

class Prime3 implements Runnable{
	boolean running = true;
	@Override
	public void run() {
		for( int i = 1; running && i<=500; i++){
			System.out.println(Thread.currentThread().getName() + "--> " + i);
		}
	}
}