【问题标题】:Spring Task Scheduler consecutive tasksSpring Task Scheduler 连续任务
【发布时间】:2016-09-11 19:55:43
【问题描述】:

我是使用 Spring Task Scheduler 执行任务的新手,所以这可能是一个基本问题。我有一个我想在实现Runnable 的类中处理的项目列表。这是我的任务类:

public class ProcessTask<T> implements Runnable {

private String item;

public ProcessTask(String item) {
    System.out.println("Starting process for " + item);
    this.item = item;
}

@Override
public void run() {
    System.out.println("Finishing task for " + item);
}

我想处理一个项目列表,每个项目在上一个任务开始后 10 秒开始。我知道我可以将每个任务设置为在前一个任务安排后 10 秒运行,但是我不想依赖它,因为其他进程可能会导致任务在 10 秒之前运行。

所以在我的主要课程中,我有以下内容:

        Date end = new Date(cal.getTimeInMillis() + 10000); // this is the time that the task should be fired off, the first one being 10 seconds after the current time

    for(String item : items) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(end);
        System.out.println("Next task fires at " + cal.getTime());
        ProcessTask task = new ProcessTask(item);
        ScheduledFuture<?> future = taskScheduler.schedule(task, end);

        end = new Date(Calendar.getInstance().getTimeInMillis() + 10000);
    }

第一个任务在代码运行 10 秒后触发,这很棒。但是其余的项目会立即安排,而不是等待 10 秒。我确实理解为什么会发生这种情况 - 因为taskScheduler.schedule 是异步的,所以 for 循环只会继续,其余的项目会在 10 秒后安排。

我尝试让主线程休眠一秒钟,并在安排下一个任务之前检查ScheduledFuture是否已完成,例如:

while(!future.isDone()) {
     Thread.sleep(1000);
     System.out.println("is future done: " + future.isDone());
}

如果我在上述块中的ScheduledFuture&lt;?&gt; future = taskScheduler.schedule(task, end); 之后立即添加此块,则future.isDone() 始终返回false,并且永远不会调用ProcessTask run() 方法。

有什么方法可以使用ScheduledFuture 来判断上一个任务是否已经结束,如果还没有,继续等待?总体上有没有更好的方法来做到这一点?提前致谢。

【问题讨论】:

  • 那么,它在某个时间开始,你想让其他的在 10 秒后开始?然后只需将分配更改为end 以不使用日历,而是使用前一个结束的值?
  • @KoosGadellaa 我正在寻找一种方法让每个任务在前一个任务运行后 10 秒开始。将来我将对其进行扩展,以便任务可能在 10 秒内完成。如果发生这种情况,我想在上一个完成点之后安排下一个 10 秒,所以我不想依赖预定的时间,而是它实际运行的时间。希望这是有道理的。
  • 编辑帖子详细说明这一点

标签: java spring multithreading taskscheduler


【解决方案1】:

所以您不知道任务何时结束,但 10 秒后,您希望下一个任务运行。因此,只有在完成该任务时才能进行规划。 所以,有一个基础抽象类,它负责管道。

public abstract class ScheduleTaskAfterRun<T> implements Runnable {
    protected void executeContent();
    private Runnable nextTask;
    private taskScheduler; // init somehow, probably by constructor...

    public void setNextTask(Runnable r) {
        nextTask = r;
    }

    @Override
    public void run() {
        executeContent();
        scheduleNextTask();
    }

    private void scheduleNextTask() {
        if(nextTask == null) {
            System.out.println("No task to handle, finished!");
            return;
        }
        Date end = new Date(Calendar.getInstance().getTimeInMillis() + 10000);
        ScheduledFuture<?> future = taskScheduler.schedule(nextTask, end);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多