1. 程式人生 > >處理定時任務,超時終止方法

處理定時任務,超時終止方法

實現功能:處理一批任務,如果某個任務的處理時間超過最大處理時間,則終止該任務的執行,繼續執行下一個任務
實現思路:三執行緒實現,處理一個任務時,啟動一個任務處理執行緒處理方案,再啟動一個定時器執行緒檢測是否超時,並通過一個同步變數保證任務時序列執行的。實現程式碼如下:

疑問:
1、在Java中,類似超時任務的處理有沒有其它的實現方式?
2、下面幾個異常會無規律地出現,想不明白其中的道理,請明白人幫忙解釋一下,
2.1、沒有執行“Normal Process”,也沒有執行“ending ok!” :為什麼interrupt後,doSomething裡沒有捕獲到異常? start task!20
ThreadID:27>>> starting!
ThreadID: MonitorThread running!
ThreadID:27>>> stoping end!
end task!20
2.2、沒有執行“ending ok!” :stop後,run裡後續處理不做了? start task!18
ThreadID:25>>> starting!
ThreadID: MonitorThread running!
ThreadID:25>>> AbNormal Proccess! processTime=18000
ThreadID:25>>> stoping end!
2.3、執行了2次“AbNormal Proccess! ”,但沒有執行“ending ok!” :為什麼會列印2次?
start task!10
ThreadID:17>>> starting!
ThreadID: MonitorThread running!
ThreadID:17>>> AbNormal Proccess! processTime=10000ThreadID:17>>> AbNormal Proccess! processTime=10000ThreadID:17>>> stoping end!
end task!10


程式碼如下:
Java code

package MultiThreadTest;

import java.util.Timer;
import java.util.TimerTask;

public class MainThread {
private Object lock = new Object();

public void waitLock() throws InterruptedException {
synchronized (lock) {
lock.wait();
}
}

public void notifyLock() {
synchronized (lock) {
lock.notify();
}
}

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) {

MainThread mainThread = new MainThread();

for (int i = 2; i <= 20; i += 2) {

System.out.println("start task!" + i);
ProccessThread proccessThread = new ProccessThread(mainThread,
i * 1000);
MonitorThread monitorThread = new MonitorThread(mainThread);
long maxProccessTime = 8 * 1000;// 每個任務的最大處理時間
Timer timer = new Timer();
timer.schedule(monitorThread, maxProccessTime);
proccessThread.start();
try {
mainThread.waitLock();
} catch (InterruptedException e) {
e.printStackTrace();
}
proccessThread.stop();
timer.cancel();
System.out.println("end task!" + i);
}
}

}

class MonitorThread extends TimerTask {
private MainThread mt;

public MonitorThread(MainThread mt) {
super();
this.mt = mt;
}

@Override
public void run() {
System.out.println("ThreadID:" + " MonitorThread running!");
mt.notifyLock();
}

}

class ProccessThread implements Runnable {
private MainThread mt;
private Thread thread;
private long processTime;

public ProccessThread(MainThread mt, long processTime) {
super();
this.mt = mt;
this.processTime = processTime;
}

private void doSomething() {
try {
// do something
// thread.sleep(100*1000); //異常情況
// thread.sleep(1*1000); //正常情況
thread.sleep(processTime); // 正常情況
System.out.println("ThreadID:" + thread.getId() + ">>> Normal Process! processTime=" + processTime);

} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("ThreadID:" + thread.getId() + ">>> AbNormal Proccess! processTime=" + processTime);
}

}

public void run() {
System.out.println("ThreadID:" + thread.getId() + ">>> starting!");
doSomething();
mt.notifyLock();
System.out.println("ThreadID:" + thread.getId() + ">>> ending ok!");

}

public void start() {
thread = new Thread(this);
thread.start();

}

public void stop() {
thread.interrupt();// 如果任務在正常時間內不能退出,認為產生interrupt,強行地退出 (run方法正常結束)
thread.stop();
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadID:" + thread.getId()
+ ">>> stoping end!");
thread = null;
}
}