Reputation: 3775
I know the traditional way to delay a thread by using sleep method. My question is supposedly i have the following:
ExecutorService threadExecutor = Executors.newFixedThreadPool(5);
Is there a way say by using ExecutorService class to have a delay between each threads without using sleep method? I mean is there a method in ExecutorService class for this purpose?
Upvotes: 13
Views: 10714
Reputation: 533442
Do you mean something like
ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
service.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
If you want three tasks, 10 seconds apart you can do
service.execute(task1);
service.schedule(task2, 10, TimeUnit.SECONDS);
service.schedule(task3, 20, TimeUnit.SECONDS);
Upvotes: 14