0

I want to implement a Executor service for Tasks which will start running after some time, For example let's assume I have these tasks;

task1 = new Task(1,10)   //Task with id 1 and will start after 10 min   
task2 = new Task(2,15)   //Task with id 2 and will start after 15 min   
task3 = new Task(3,5)    //Task with id 3 and will start after 5 min   
task4 = new Task(4,30)   //Task with id 4 and will start after 30 min   

when I submit all of those to an executor service, I should get the following result;

(after 5 min):
...task 3 is running...
(after 10 min)
...task 1 is running...
(after 15 min)
...task 2 is running...
(after 30 min)
...task 4 is running...

I could find out how can I implement this. Can you help me about it?

Emre EREN
  • 63
  • 1
  • 8

1 Answers1

2

Take a look at ScheduledThreadPoolExecutor. Here's an example showing how to schedule a thread to run in 5 minutes.

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
Thread thread = new Thread();
executor.schedule(thread, 5, TimeUnit.MINUTES);
Kaan
  • 5,434
  • 3
  • 19
  • 41