基于ScheduledExecutorService的并发定时任务处理能力测试

测试代码

定时器类

package business.util; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import transfer.IUserParser; public class MyTimer { private static ScheduledExecutorService ses = Executors .newSingleThreadScheduledExecutor();

// .newScheduledThreadPool(10); private MyTimer() { } / * 设置某一parser正在执行脚本的超时时间 * * @param parser * 进行超时控制的解析器 * @param timeout * 超时时间,单位:秒 * @return */ public static ScheduledFuture<?> set(IUserParser parser, int timeout) { CloseRutine routine = new CloseRutine(parser); ScheduledFuture<?> job = ses.schedule(routine, timeout, TimeUnit.SECONDS); return job; } / * 清除计时器 * * @param unit * @return / public static boolean cancel(ScheduledFuture<?> job) { return job.cancel(true); } / * 关闭此计时器(进程) / public static void shutdown() { ses.shutdown(); } } class CloseRutine implements Runnable { private final IUserParser parser; CloseRutine(IUserParser parser) { this.parser = parser; } @Override public void run() { parser.closePeer(); } }

posted on 2014-03-18 08:04  leechau  阅读(362)  评论(0编辑  收藏  举报

导航