?? twophasetermination.htm
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>Two-phase Termination 模式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">Design Pattern: Two-phase Termination 模式</a></h1>
Two-phase
Termination直譯的話是“兩相終止”,不過就這個模式而言,該譯作“兩階段終止”比較適當,想像您有一個執(zhí)行緒正在周期性的運作,在“運作階
段”您送出了停止執(zhí)行緒的請求,這時候執(zhí)行緒不該慌張的馬上終止目前的工作,而是先完成這一次周期的工作,然后進入“善后階段”完成一些善后的工作,例如
關(guān)閉檔案或網(wǎng)路串流,所謂的兩階段終止,即中止“運作階段”,并完成“善后階段”,完整的完成執(zhí)行緒的工作。<br>
<br>
以Java的Thread終止而言,不建議您直接使用stop()方法來終止執(zhí)行緒,stop()方法會丟出ThreadDeath例外強迫執(zhí)行緒終止,即使執(zhí)行緒正在運作階段或執(zhí)行至synchronized區(qū),如果您要終止執(zhí)行緒,建議自行實作,例如:<br>
<div style="margin-left: 40px;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public class SomeThread extends Thread {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> private boolean isTerminated = false;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public void terminate() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> isTerminated = true;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public void run() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> while(!isTerminated) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> // ... some statements</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br>
</div>
<br>
考慮到有時執(zhí)行緒可能會執(zhí)行至sleep()或wait()而進入Not
Runnable狀態(tài),使用上面的方法可能會延遲終止的請求,因而可以在要求終止時再呼叫interrupt()方法,這會丟出
InterruptedException,而使得執(zhí)行緒從Not Runnable狀態(tài)中離開,因此可以改變一下程式:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public class SomeThread extends Thread {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> private boolean isTerminated = false;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -