Question

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?

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top