`
wxb_love
  • 浏览: 94998 次
  • 性别: Icon_minigender_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;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics