简体   繁体   中英

how to make a task x run after a scheduled task y

in my scenario i need to schedule a chain of tasks. eg task a, b and c should start running at 1 o'clock but in the order ive inserted them. task a should start at 1 o'clock and task b should start after task a is finished, whenever that may be. Task c also starts only after task b has finished.

I would have hoped that springs Taskscheduler could just schedule a list of runnables, but i can only schedule on Runnable :

taskScheduler.schedule(task, cronTrigger()));

How can i do something like this :

taskScheduler.schedule(taskList, cronTrigger()));

Any idea?

A reasonable approach would probably be to create a basic implementation of a Runnable that runs a list of Runnables, and then to schedule that as your task, eg:

public class RunnableList implements Runnable {
    private final List<Runnable> delegates;

    public RunnableList(List<Runnable> delegates) {
        this.delegates = new ArrayList<Runnable>(delegates);
    }

    @Override
    public void run() {
        for (Runnable job : delegates) {
            job.run();
        }
    }
}

If you use an ExecutorService with only 1 Thread, you can use invokeAll on a list of Callables. The way the Executor is designed and since there is only one Thread to process those tasks, those tasks are going to be processed in the given order.

If you must use Runnables, you need to loop-add them in the correct order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM