Java程式設計核心技術之——執行緒操作

Java中常見的執行緒操作包括執行緒休眠、執行緒等待(插入)、執行緒中斷,具體案例如下:

ThreadOptCases.java:

import java。util。Vector;

import java。util。concurrent。ExecutorService;

import java。util。concurrent。Executors;

import java。util。concurrent。TimeUnit;

/**

* 執行緒類定義,用於執行緒操作(執行緒休眠)應用案例演示

*/

class ThreadClass2 extends Thread

{

//計數器,用於記錄執行緒執行情況

private int count = 10;

/**

* 重寫Thread。run()方法

*/

public void run()

{

while (true)

{

//執行緒休眠10毫秒

try

{

Thread。sleep(10);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e。printStackTrace();

}

//列印count變數

System。out。print(“Count=”+count+“ ”);

//使count變數自減,當自減為0時,退出迴圈

if (0 == ——count)

{

//列印換行

System。out。println();

return;

}

}//while (true)

}//public void run()

}//class ThreadClass2 extends Thread

/**

* 執行緒類定義,用於執行緒操作(執行緒插入)應用案例演示

*/

class ThreadClass3 extends Thread

{

//計數器,用於記錄執行緒執行情況

private int count = 10;

//被插入的執行緒物件

private ThreadClass4 joinedThread;

/**

* 構造方法

* @param joined:[IN]將被插入的執行緒

*/

ThreadClass3(ThreadClass4 joined)

{

joinedThread = joined;

//。。

}//ThreadClass3(ThreadClass4 joined)

/**

* 重寫Thread。run()方法

*/

public void run()

{

while (true)

{

try

{

//插入執行緒(將被先執行)

joinedThread。join();

//執行緒休眠10毫秒

Thread。sleep(10);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e。printStackTrace();

}

//列印count變數

System。out。print(“Count3=”+count+“ ”);

//使count變數自減,當自減為0時,退出迴圈

if (0 == ——count)

{

//列印換行

System。out。println();

return;

}

}//while (true)

}//public void run()

}//class ThreadClass3 extends Thread

/**

* 執行緒類定義,用於執行緒操作(執行緒插入)應用案例演示

*/

class ThreadClass4 extends Thread

{

//計數器,用於記錄執行緒執行情況

private int count = 10;

/**

* 重寫Thread。run()方法

*/

public void run()

{

while (true)

{

//執行緒休眠10毫秒

try

{

Thread。sleep(10);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e。printStackTrace();

}

//列印count變數

System。out。print(“Count4=”+count+“ ”);

//使count變數自減,當自減為0時,退出迴圈

if (0 == ——count)

{

//列印換行

System。out。println();

return;

}

}//while (true)

}//public void run()

}//class ThreadClass4 extends Thread

/**

* 執行緒類定義,用於執行緒操作(執行緒中斷:標記變數)應用案例演示

*

*/

class ThreadClass5 extends Thread

{

//計數器,用於記錄執行緒執行情況

private int count = 10;

//用於執行緒中斷控制

private boolean isContinue = true;

/**

* 重寫Thread。run()方法

*/

public void run()

{

while (true)

{

try

{

//執行緒休眠10毫秒

Thread。sleep(10);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e。printStackTrace();

}

//執行緒結束

if(!isContinue)

{

//列印換行

System。out。println();

break;

//。。

}

//列印count變數

System。out。print(“Count=”+count+“ ”);

//使count變數自減,當自減為0時,退出迴圈

if (0 == ——count)

{

//列印換行

System。out。println();

return;

}

}//while (true)

}//public void run()

/**

* 停止執行緒執行

*/

public void StopRun()

{

isContinue = false;

}//public void StopRun()

}//class ThreadClass5 extends Thread

/**

* 執行緒類定義,用於執行緒操作(執行緒中斷:中斷方法)應用案例演示

*/

class ThreadClass6 extends Thread

{

//計數器,用於記錄執行緒執行情況

private int count = 10;

/**

* 重寫Thread。run()方法

*/

public void run()

{

while (true)

{

try

{

//執行緒休眠10毫秒

Thread。sleep(10);

}

catch (InterruptedException e)

{

// TODO Auto-generated catch block

//e。printStackTrace();

//列印換行

System。out。println();

//執行緒中斷後跳出

break;

}

//列印count變數

System。out。print(“Count=”+count+“ ”);

//使count變數自減,當自減為0時,退出迴圈

if (0 == ——count)

{

//列印換行

System。out。println();

return;

}

}//while (true)

}//public void run()

}//class ThreadClass6 extends Thread

class ThreadClass6_1 extends Thread

{

//計數器,用於記錄執行緒執行情況

private int count = 0;

/**

* 重寫Thread。run()方法

*/

public void run()

{

while (true)

{

//判斷是否為中斷狀態(用該方法時,執行緒體中不能同時出現Thread。wait/join/sleep等方法,否則

//執行緒將丟擲中斷異常並清除中斷狀態,從而導致該判斷失效)

if(Thread。currentThread()。isInterrupted())

{

//列印換行

System。out。println();

//執行緒中斷後跳出

break;

}

count++;

//列印count變數

System。out。print(“Count=”+count+“ ”);

}//while (true)

}//public void run()

}//class ThreadClass6_1 extends Thread

/**

* 執行緒操作案例類

*/

public class ThreadOptCases

{

//執行緒建立,執行控制時間

private static final long CREATE_CTRL_TIME = 20;

/**

* 等待一會時間,用於執行順序控制

* @param millis :[IN]等待的時間(單位:毫秒)

*/

protected static void waitForMoment(long millis)

{

try

{

Thread。sleep(millis);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e。printStackTrace();

}

}//protected static void waitForMoment(

public static void main(String[] args)

{

System。out。println(“/////////////////執行緒操作(執行緒休眠),應用案例演示/////////////////”);

//例項化執行緒物件

ThreadClass2 thread2 = new ThreadClass2();

//執行緒啟動

thread2。start();

//等待一會時間,讓執行緒結束

waitForMoment(200);

System。out。println(“/////////////////執行緒操作(執行緒等待,方式1:插入執行緒),應用案例演示/////////////////”);

//一個執行緒等待另一個執行緒執行完畢

{

//例項化執行緒物件

ThreadClass4 thread4 = new ThreadClass4();

//例項化執行緒物件

ThreadClass3 thread3 = new ThreadClass3(thread4);

//如果執行緒執行的過程中發現被插入的執行緒也開始執行,則當前執行緒暫停執行,等被插入的執行緒優先執行完畢後,該執行緒再執行

thread3。start();

//this。waitForMoment(50);

thread4。start();

//等待一會時間,讓執行緒結束

waitForMoment(300);

}

//一個執行緒等待其它多個執行緒執行完畢

{

Vector threads = new Vector();

for (int i = 0; i < 5; i++)

{

Thread iThread = new Thread(new Runnable()

{

public void run()

{

try

{

//模擬子執行緒任務

Thread。sleep(10);

}

catch (InterruptedException e)

{

//。。

}

System。out。println(“子執行緒” + Thread。currentThread() + “執行完畢”);

}

});

threads。add(iThread);

iThread。start();

}

for (Thread iThread : threads)

{

try

{

//等待所有執行緒執行完畢

iThread。join();

}

catch (InterruptedException e)

{

e。printStackTrace();

}

}

System。out。println(“主執行緒執行!”);

}

System。out。println(“/////////////////執行緒操作(執行緒等待,方式2:使用執行緒池),應用案例演示/////////////////”);

//定義一個緩衝執行緒池,執行緒池的大小會根據任務自動變化

ExecutorService threadPool = Executors。newCachedThreadPool();

//將被等待的執行緒加入執行緒池

for (int i = 0; i < 5; i++)

{

threadPool。execute(new Runnable()

{

public void run()

{

try

{

//模擬子執行緒任務

Thread。sleep(10);

}

catch (InterruptedException e)

{

//。。

}

System。out。println(“子執行緒” + Thread。currentThread() + “執行完畢”);

}

});

}//for (int i = 0; i < 5; i++)

//關閉執行緒池(不會立即終止執行緒池,而是要等所有任務執行完後才終止,但再也不會接受新的任務)

threadPool。shutdown();

try

{

//一直阻塞、直到所有執行緒任務完成執行(無論當前執行緒是否發生超時或中斷)

threadPool。awaitTermination(10, TimeUnit。SECONDS);

}

catch (InterruptedException e)

{

e。printStackTrace();

}

System。out。println(“主執行緒執行!”);

System。out。println(“/////////////////執行緒操作(執行緒中斷,方式1:標記變數),應用案例演示/////////////////”);

//例項化執行緒物件

ThreadClass5 thread5 = new ThreadClass5();

//執行緒啟動

thread5。start();

//讓執行緒執行一會

waitForMoment(50);

//停止執行緒執行(標記變數)

thread5。StopRun();

//等待一會時間,讓執行緒結束

waitForMoment(50);

System。out。println(“/////////////////執行緒操作(執行緒中斷,方式2:中斷方法-捕獲異常),應用案例演示/////////////////”);

//例項化執行緒物件

ThreadClass6 thread6 = new ThreadClass6();

//執行緒啟動

thread6。start();

//讓執行緒執行一會

waitForMoment(50);

//停止執行緒執行(中斷方法)

thread6。interrupt();

//等待一會時間,讓執行緒結束

waitForMoment(50);

System。out。println(“/////////////////執行緒操作(執行緒中斷,方式2:中斷方法-判斷狀態),應用案例演示/////////////////”);

//例項化執行緒物件

ThreadClass6_1 thread6_1 = new ThreadClass6_1();

//執行緒啟動

thread6_1。start();

//讓執行緒執行一會

waitForMoment(2);

//停止執行緒執行(中斷方法)

thread6_1。interrupt();

//等待一會時間,讓執行緒結束

waitForMoment(50);

//。。

}//public static void main(

}//public class ThreadCases

執行以上程式碼,可得到如下結果:

Java程式設計核心技術之——執行緒操作