多線程超時處理的方法

package cn.mytest;

import java.util.ArrayList;
import java.util.HashMap;


/**
* @Description: 線程監控超時的工具類
* @author
* @date 2014-9-18 下午04:47:12
*/
public class ThreadWathcher extends Thread {


private static ThreadWathcher watcher;

/**
* 存放對應的線程跟開始執行的時間
*/
private HashMap<Thread, Long> threadBornTimeCollection;

/**
* 需要被中斷的線程
*/
private ArrayList<Thread> toRemoveThreads;
/**
* 超時時間
*/
private long timeOutMills;
/**
* 間隔掃描時間
*/
private long periodMills;


private ThreadWathcher() {
/**
* 設置線程爲守護線程 以致主線程停止的時候守護線程也自動終止
*/
this.setDaemon(true);
threadBornTimeCollection = new HashMap<Thread, Long>();
toRemoveThreads = new ArrayList<Thread>();
}

/**
* 配置的超時時間必須是間隔檢測時間的倍數+3 比如超時時間是1000則 period應該是503
* @param timeout
* @param period
* @return
*/
public static ThreadWathcher getInstance(long timeout, long period) {

if (watcher == null) {
watcher = new ThreadWathcher();
watcher.timeOutMills = timeout;
watcher.periodMills = period;
}

return watcher;
}


public int register(Thread thread) {

threadBornTimeCollection.put(thread, System.currentTimeMillis());

return threadBornTimeCollection.size();
}


@Override
public void run() {
super.run();

while (true) {// 守護線程
try {
Thread.sleep(periodMills);// 每隔periodMills秒檢查一次
for (Thread e : threadBornTimeCollection.keySet()) {// 遍歷已經註冊過超時處理的線程集合
if (Math.abs(threadBornTimeCollection.get(e)
- System.currentTimeMillis()) > timeOutMills
&& e.isAlive()) {// 超時
toRemoveThreads.add(e);// 添加到超時線程集合中
}
}
for (Thread e : toRemoveThreads) {// 遍歷超時線程集合
threadBornTimeCollection.remove(e);// 從超時集合中移除
e.interrupt();// 中斷超時線程
}


} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (toRemoveThreads.size() > 0) {
System.out.println("清空超時線程集合");
toRemoveThreads.clear();// 清空超時線程集合
}
}
}
}
}


package cn.mytest;

import java.util.ArrayList;
import java.util.HashMap;

public class Main {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Begin here...");

ThreadWathcher wacher = ThreadWathcher.getInstance(1000, 503);
wacher.start();
Thread a = new Thread() {

@Override
public void run() {
super.run();
int n = 0;
try {
System.out.println("A線程執行中-----");
Thread.sleep(1100);
System.out.println("A線程執行完成.....");
/* while (n < 1 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread a..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("線程因超時被終止...線程名" + this.getName());
}


}


};
a.setName("a");
wacher.register(a);
a.start();
Thread b = new Thread() {




@Override
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("B線程執行中-----");
Thread.sleep(900);
System.out.println("B線程執行完成.....");
/* while (n < 5 && !Thread.interrupted()) {


Thread.sleep(1000);
n++;
System.out.println("In thread b..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("線程因超時被終止...線程名" + this.getName());
}


}


};
b.setName("b");
b.start();
wacher.register(b);
Thread c = new Thread() {


public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("C線程執行中-----");
Thread.sleep(1200);
System.out.println("C線程執行完成.....");
/* while (n < 12 && !Thread.interrupted()) {


Thread.sleep(1000);
n++;
System.out.println("In thread c..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("線程因超時被終止...線程名" + this.getName());
}


}


};
c.setName("c");
c.start();
wacher.register(c);
Thread d = new Thread() {



public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("D線程執行中-----");
Thread.sleep(500);
System.out.println("D線程執行完成.....");
/* while (n < 15 && !Thread.interrupted()) {


Thread.sleep(1000);
n++;
System.out.println("In thread d..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("線程因超時被終止...線程名" + this.getName());
}


}


};
d.setName("d");
d.start();
wacher.register(d);

}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章