问题
Is their a way to schedule a thread pool using ExecutorService , in lines similar to thread.sleep()
My current code looks something like
Executors.newScheduledThreadPool(poolSize);
public void run() {
try {
pool.execute(new Worker());
}
But I want to call the run method, only after some time interval. Can someone let me know how to do this?
回答1:
This can be achieved using ScheduledThreadPoolExecutor.
Sample code
pool = new ScheduledThreadPoolExecutor(10);
pool.scheduleWithFixedDelay(new Thread(), 100,200, TimeUnit.MILLISECONDS);
The 'run()' method of the 'Thread()' class will be called at a regular intervals of 200 milliseconds & its first execution will be after 100 ms
来源:https://stackoverflow.com/questions/20988664/calling-executorservice-after-some-interval-of-time