如何使用ScheduledExecutorService随时在一周内每周五运行特定任务?

时间:2014-03-01 18:00:20

标签: java multithreading scheduledexecutorservice

我试图在任何时间每周五每周五完成一项任务。所以我决定使用ScheduledExecutorService,但到目前为止,我已经看到了示例,显示了如何每隔几分钟运行一次任务。

以下是我每天早上5点开始运行的代码。如何在一周内的每个星期五使用它来运行任务?

public static void main(String[] args) {

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
    Date aDate = new Date();
    Calendar with = Calendar.getInstance();
    with.setTime(aDate);

    int hour = with.get(Calendar.HOUR_OF_DAY);
    int intDelayInHour = hour < 5 ? 5 - hour : 24 - (hour - 5);

    System.out.println("Current Hour: " + hour);
    System.out.println("Comuted Delay for next 5 AM: " + intDelayInHour);

    scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
            try {
                getDataFromDatabase();
            } catch (Exception ex) {
                ex.printStackTrace(); // or loggger would be better
            }
        }
    }, intDelayInHour, 24, TimeUnit.HOURS);
}

protected static void getDataFromDatabase() {
    // TODO Auto-generated method stub

}

任何人都可以举例说明我该怎么做?

2 个答案:

答案 0 :(得分:1)

你必须检查它今天是哪一天。

然后将延迟设置为下周五(假设它是星期二,然后设置3天延迟,或者如果要在不同时间设置它,则使用小时)。

然后使用7天(或数小时内等效)。

编辑:

根据要求,您可以执行类似的操作。

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(DAY_OF_WEEK);
int delayInDays = dayToDelay.get(dayOfWeek);

scheduler.scheduleAtFixedRate(new Runnable() {
    public void run() {
        try {
            getDataFromDatabase();
        } catch (Exception ex) {
            ex.printStackTrace(); // or loggger would be better
        }
    }
}, delayInDays, 7, TimeUnit.DAYS);

这应该在每个星期五执行时执行任务。

答案 1 :(得分:0)

每个星期一的上午11点运行程序

public static void main(String args[]){
    new WeeklyReportService();
}

WeeklyReportService.java

public class WeeklyReportService{

public WeeklyReportService(){
    this.startScheduler();
}

private void startScheduler(){
    Calendar with = Calendar.getInstance();
    Map<Integer, Integer> dayToDelay = new HashMap<Integer, Integer>();
            dayToDelay.put(Calendar.FRIDAY, 2);
            dayToDelay.put(Calendar.SATURDAY, 1);
            dayToDelay.put(Calendar.SUNDAY, 0);
            dayToDelay.put(Calendar.MONDAY, 6);
            dayToDelay.put(Calendar.TUESDAY, 5);
            dayToDelay.put(Calendar.WEDNESDAY, 4);
            dayToDelay.put(Calendar.THURSDAY, 3);
            int dayOfWeek = with.get(Calendar.DAY_OF_WEEK);
            int hour = with.get(Calendar.HOUR_OF_DAY);
            int delayInDays = dayToDelay.get(dayOfWeek);
            int delayInHours = 0;
            if(delayInDays == 6 && hour<11){
                delayInHours = 11 - hour;
            }else{
                delayInHours = delayInDays*24+((24-hour)+11);
            }
     ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);            
     scheduler.scheduleAtFixedRate(new WeeklyTask(), delayInHours,
                                   179, TimeUnit.HOURS);
}

WeeklyTask.java

Public class WeeklyTask implements Runnable {


@Override
public void run() {
    System.out.println("start of weekly report");
    /*Your Program to run*/
    System.out.println("end of weekly report");
}

}

希望这会有所帮助!您可以将其应用于任何一天。您的情况是,星期五上午11点将是

            dayToDelay.put(Calendar.FRIDAY, 6);
            dayToDelay.put(Calendar.SATURDAY, 5);
            dayToDelay.put(Calendar.SUNDAY, 4);
            dayToDelay.put(Calendar.MONDAY, 3);
            dayToDelay.put(Calendar.TUESDAY, 2);
            dayToDelay.put(Calendar.WEDNESDAY, 1);
            dayToDelay.put(Calendar.THURSDAY, 0);