添加延迟以使用 ScheduledExecutorService 对象启动执行线程

Add delay to start execution thread with ScheduledExecutorService object

我正在实现一个线程池来发送多个 get snmp。我正在使用 Callable 对象:

final ScheduledExecutorService executor = Executors.newScheduledThreadPool(size);
final Map<String, Future< >> map = new HashMap<String, Future< >>();
for (final String key : groupMap.keySet()) {
    final ObjectCallable objectCallable = new ObjectCallable(par1, par2, par3);
    try {
         Thread.sleep(50);
    } catch (final InterruptedException e) {
           LOGGER.error("Error delay in the submit Callable" + e.getMessage());
            }
    final Future< > result = executor.submit(objectCallable);
}

我需要在线程之间添加执行延迟。我在提交前添加了一个睡眠,这是正确的吗?有人知道其他方法吗?

ScheduledExecutorServiceschedule 方法(及其重载)接受一个 delay 参数,该参数似乎准确定义了您要查找的内容,如:

delay - the time from now to delay execution

在您的示例中,您正在做的是在提交您的 Callable 执行之前人为地在当前执行线程上休眠(您正在通过调用 ExecutorService#submit 执行此操作,因此没有利用ScheduledExecutorService).

的具体方法