我尝试使用ScheduledExecutorService(在scheduleMatch方法中)安排任务。我看到我的Task类的构造函数被调用,但从不调用Run方法。我也有一个scheduleAtFixedRate,一个工作正常,它每30秒调用一次。请参阅下面的代码,我错过了什么或为什么这段代码不起作用:
MatchTaskScheduler
public class MatchTaskScheduler {
private final ScheduledExecutorService fScheduler;
private final ScheduledExecutorService matchScheduler;
private static final int NUM_THREADS = 1;
private static final int MATCH_SCHEDULER_NUM_THREADS = 10;
private static MatchTaskScheduler taskScheduler;
private MatchTaskScheduler(){
fScheduler = Executors.newScheduledThreadPool(NUM_THREADS);
matchScheduler = Executors.newScheduledThreadPool(MATCH_SCHEDULER_NUM_THREADS);
}
public static MatchTaskScheduler getInstance() {
if(taskScheduler == null) {
taskScheduler = new MatchTaskScheduler();
}
return taskScheduler;
}
public void scheduleMatchScheduler() {
//This one is working correct
fScheduler.scheduleAtFixedRate(new ScheduleMatchTask(), 150, 30, TimeUnit.SECONDS);
}
public void scheduleMatch(Match match) {
Validate.notNull(match, "match of plublic method scheduleMatch shouldn't be null");
Date scheduledDate = match.getDate();
Logger.info(this, "Schedule match at: " + scheduledDate);
if (null == scheduledDate || !scheduledDate.after(new Date())) {
Logger.error(this, "Match with matchId: " + match.getId() + " isn\'t scheduled because date is in the past");
return;
}
long startTimeInSeconds = (scheduledDate.getTime() / 1000) - (System.currentTimeMillis() / 1000);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Logger.info(this, "Current date: " + sdf.format(new Date()));
Logger.info(this, "Match with matchId: " + match.getId() + " scheduled for: " + sdf.format(scheduledDate));
try {
matchScheduler.schedule(new MatchTask(match), startTimeInSeconds, TimeUnit.SECONDS);
} catch (RejectedExecutionException e) {
Logger.error(this, e.toString(), e);
} catch (NullPointerException e1) {
Logger.error(this, e1.toString(), e1);
}
Logger.info(this, "Match will start in " + startTimeInSeconds + " seconds.");
}
}
MatchTask
public class MatchTask extends TimerTask {
private Match match;
public MatchTask(Match match) {
Validate.notNull(match, "match of plublic constructor MatchTask shouldn't be null");
Logger.info(this, "Set match for Runnable + " + match.getId());
this.match = match;
}
@Override
public void run() {
System.out.println("Match ID: " + match.getId() + ", current time: " + new Date().getSeconds());
}
}
这是我在日志文件中看到的,在08:33:00,日志文件中没有任何内容。接下来是在8:33:04调用scheduleAtFixedRate时。
[21/10/14 08:32:34:631 CEST] INFO task.MatchTaskScheduler: Schedule match at: 2014-10-21 08:33:00.0
[21/10/14 08:32:34:631 CEST] INFO task.MatchTaskScheduler: Current date: 21-10-2014 08:32:34
[21/10/14 08:32:34:632 CEST] INFO task.MatchTaskScheduler: Match with matchId: 002 scheduled for: 21-10-2014 08:33:00
[21/10/14 08:32:34:632 CEST] INFO task.MatchTask: Set match for Runnable + 002
[21/10/14 08:32:34:632 CEST] INFO task.MatchTaskScheduler: Match will start in 26 seconds.
解决了:问题是System.out.println,当我用Logger.info替换它时,它工作正常。