毎週木曜日の午後 7 時以降にいつでもジョブを実行するようにスケジュールしようとしています。しかし、これまでのところ、以下のコードを使用して木曜日に実行することはできますが、午後 7 時以降は実行できません。
私はScheduledExecutorsService
これに使用しています。木曜日の午後 7 時以降に実行する方法はありますか?
private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
final ArrayList<Callable<Void>> tasks = Lists.newArrayList(new TestBImpl(), new TestAImpl());
Calendar with = Calendar.getInstance();
Map<Integer, Integer> dayToDelay = new HashMap<Integer, Integer>();
dayToDelay.put(Calendar.FRIDAY, 0);
dayToDelay.put(Calendar.SATURDAY, 6);
dayToDelay.put(Calendar.SUNDAY, 5);
dayToDelay.put(Calendar.MONDAY, 4);
dayToDelay.put(Calendar.TUESDAY, 3);
dayToDelay.put(Calendar.WEDNESDAY, 2);
dayToDelay.put(Calendar.THURSDAY, 1);
int dayOfWeek = with.get(Calendar.DAY_OF_WEEK);
int delayInDays = dayToDelay.get(dayOfWeek);
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
executorService.invokeAll(tasks);
} catch (Exception ex) {
ex.printStackTrace(); // or loggger would be better
}
}
}, 0, delayInDays, TimeUnit.DAYS);
どんな提案でも大いに役立ちます。タスクを実行するマルチスレッドの方法を使用する必要があります。私のtasks
では、現在、並行して実行しているクラスが2つしかないため、3つ以上のクラスがあります。