Calling ExecutorService after some interval of time

无人久伴 提交于 2020-01-05 03:01:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!