Java 如何使用ScheduledExecutorsService在周四晚上7点之后运行特定任务?

Java 如何使用ScheduledExecutorsService在周四晚上7点之后运行特定任务?,java,multithreading,executorservice,scheduledexecutorservice,Java,Multithreading,Executorservice,Scheduledexecutorservice,我正试图安排一个作业在每周四晚上7点以后运行。但到目前为止,通过下面的代码,我可以让它在周四运行,但任何时候都不能在晚上7点之后运行 我正在为此使用ScheduledExecutorService。有什么办法可以让我在周四晚上7点后跑步吗 private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); final ArrayList<Callable&

我正试图安排一个作业在每周四晚上7点以后运行。但到目前为止,通过下面的代码,我可以让它在周四运行,但任何时候都不能在晚上7点之后运行

我正在为此使用ScheduledExecutorService。有什么办法可以让我在周四晚上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);

任何建议都会大有帮助。我需要使用多线程的方式来执行我的任务。在我的任务中,我将有两个以上的类,因为现在我只有两个并行执行的类。

我会让调度程序在每小时运行任务,任务将使用日历确定日期和时间,并且仅在星期四晚上7点执行

    scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
            Calendar c = Calendar.getInstance();
            if (c.get(Calendar.HOUR_OF_DAY) == 19 && c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
                try {
                    executorService.invokeAll(tasks);
                } catch (Exception ex) {
                    ex.printStackTrace(); // or loggger would be better
                }
            }
        }
    }, 0, 1, TimeUnit.HOURS);

但这并不意味着它会每小时执行我的任务,对吗?对不起,我有点搞混了。如果可能的话,你能提供一个例子让我明白吗?代码示例也应该检查Calendar.AM\u PM。@Michael Easter我同意