在一定时间间隔后调用ExecutorService [英] Calling ExecutorService after some interval of time

查看:78
本文介绍了在一定时间间隔后调用ExecutorService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是他们使用ExecutorService调度线程池的一种方式,类似于thread.sleep()

Is their a way to schedule a thread pool using ExecutorService , in lines similar to thread.sleep()

我当前的代码看起来像

Executors.newScheduledThreadPool(poolSize);
 public void run() { 
 try {
     pool.execute(new Worker());
 } 

但是我只想在一定时间间隔后调用run方法.有人可以让我知道该怎么做吗?

But I want to call the run method, only after some time interval. Can someone let me know how to do this?

推荐答案

可以使用ScheduledThreadPoolExecutor来实现.

This can be achieved using ScheduledThreadPoolExecutor.

示例代码

pool = new ScheduledThreadPoolExecutor(10);
pool.scheduleWithFixedDelay(new Thread(), 100,200, TimeUnit.MILLISECONDS);

"Thread()"类的"run()"方法将以200毫秒&的常规间隔被调用.它的第一次执行将在100毫秒后

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

这篇关于在一定时间间隔后调用ExecutorService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆