package com.meehu.majiang.handle.test;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import io.netty.util.concurrent.DefaultThreadFactory;
public class TestTimer extends TimerTask {
private ScheduledFuture<?> timer;
public void start() {
final int timeMillis = 1000; // 执行间隔/每秒
ScheduledExecutorService t = new ScheduledThreadPoolExecutor(1, new DefaultThreadFactory("TestPool"));
timer = t.scheduleAtFixedRate(this, timeMillis, timeMillis, TimeUnit.MILLISECONDS);
}
@Override
public void run() {
try {
} catch (Exception e) {
timer.cancel(false); // 出错后,重启这个定时任务。
TestTimer t = new TestTimer();
t.start();
}
}
}