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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top