سؤال

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