问题标签 [scheduledexecutorservice]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
871 浏览

multithreading - 如果仅使用声明进行初始化,则 ExecutorService 会抛出 RejectedExecutionException

我得到了一些代码,如果我只使用带有初始化的声明,如

这会让我java.util.concurrent.RejectedExecutionException兴奋

但是如果我之前再次初始化

一切运行正常。这是为什么?我发现的所有示例都不会在调度之前每次都执行 SingleThreadExecutor。我认为这只是设置池大小。它应该在声明期间被初始化?我很困惑:)谢谢

0 投票
2 回答
4636 浏览

java - ScheduledExecutorService doesn't works during my junit test

I am working on writing some junit test for my spring application. Below is my junit test that that calls afterPropertiesSet method of my InitializeFramework class that implements InitializingBean interface.

Below is the flow where my junit test calls afterPropertiesSet method and then that method will call initializeModel method within the same class, and then that method has a scheduler which will call getBundlesInfo method every few minutes. But somehow during my junit, getBundlesInfo method is not getting called at all.

Can anybody help me with this? What wrong I am doing here? but during my application run, this flow works perfectly fine and getBundlesInfo gets called... Only during junit it is not working..

0 投票
1 回答
471 浏览

threadpool - 在 ScheduledThreadPoolExecutor 关闭后,future.get 会起作用吗?

我们使用 ScheduledThreadPoolExecutor 并在提交作业后立即调用shutdown。因为根据文档 Shutdown 不会杀死提交的任务,正在运行的任务并允许它完成。

问题是shutdown后我们能不能继续使用ScheduledThreadPoolExecutor提交返回的future对象。

0 投票
1 回答
1356 浏览

android - 为什么 ScheduledExecutorService.schedule(Runnable r, Long delay, TimeUnit t) 会忽略延迟并立即运行?

我有一个 ScheduledExecutorService,我想用它来安排具有一定延迟的 Runnable 的执行。但是,当我调用它的 schedule 方法时,延迟会被完全忽略,Runnable 会立即执行。这是我的代码:

我的 ScheduledExecutorService 构造函数:

这是对其 schedule 方法的调用(由 Logs 包围):

日志让我看到“在计划之前”和“设置清除...”之间只有 20-30 毫秒的延迟,而不是我预期的 1000 毫秒。

schedule 方法的文档如下:

创建并执行在给定延迟后启用的一次性操作。

在这种情况下,“启用”一词的确切含义是什么?

任何帮助将不胜感激。

编辑:即使使用 Long (1000L),代码似乎也会立即执行。

0 投票
2 回答
2649 浏览

java - Executors Factory 方法 newScheduledThreadPool 总是返回相同的线程池

根据 Java 规范: public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) “创建一个线程池,可以安排命令在给定延迟后运行,或定期执行。”

但是根据我的经验,即使我创建了两个单独ScheduledExecutorService调用newScheduledThreadPool(int corePoolSize)工厂方法两次并安排两个完全不同的 Callable 任务,两个线程同步运行,即一个等待另一个。考虑到已经创建了两个单独的线程池,它们应该同时运行。

输出:

0 投票
1 回答
1583 浏览

java - 如何验证 ScheduledExecutorService 已启动?

描述

任务 A 正在运行,可以持续 2 秒到 60 多秒。

在 A 运行时,我安排了一个 ExecutorService,它在 10 秒(延迟)后每 5 秒打印一次“运行”(固定速率)。

一旦 A 完成,我希望打印“A 完成”。但前提是我的 ScheduledExecutorService 任务已经开始,这意味着它至少打印了一次“正在运行”。

  • 如果 A 在 10 秒之前完成,我不会打印任何内容。
  • 如果 A 花费超过 10 秒,我每 5 秒打印一次“正在运行”,然后在 A 完成后“完成”。

如果不在 schduled 中使用布尔标志,我怎么能做到这一点runnable?有没有办法检查我的服务是否已启动?

0 投票
1 回答
1100 浏览

java - 了解 ScheduledThreadPoolExecutor

我知道我必须使用它而不是java.util.Timer因为各种原因。所以,为了研究这个,我正在查看文档,我有几个问题:

  1. 如何scheduleWithFixedDelay()工作?我的理解是:它在给定延迟后首先执行任务。一旦任务完成,它会等待指定的时间,然后再次执行任务。
  2. 当我提交的任务scheduleAtFixedRate()执行时间比指定的延迟时间长时会发生什么?就像我希望任务每 5 秒执行一次,但需要 10 秒才能完成。 我的理解是任务将被保存在队列中,一旦核心线程可用就会执行
  3. 这是我对如何scheduleWithFixedDelay()scheduleAtFixedRate()不同的理解: scheduleWithFixedDelay()等待任务完成执行,等待指定的时间,然后再次触发任务,因为scheduleAtFixedRate将继续触发任务而不关心它是否完成。正确的?
0 投票
1 回答
75 浏览

java - 在 Web 应用程序中运行 ScheduledExecutorService 是否有任何影响

我正在考虑在 java webapplication 中以预定义的时间间隔运行任务,我听说 webapplication 中的启动线程由于某些原因是不正确的。我想知道如果在 Web 应用程序中这样做 ScheduledExecutorService 会有什么影响。

0 投票
2 回答
7671 浏览

java - ScheduledExecutorService 等待任务完成

我正在创建一个新线程来检查文件夹中的新文件,然后休眠一段定义的时间。

我的偏好是使用 ScheduledExecutorService,但是,我找不到任何文档来说明这是否等待当前正在运行的任务完成后再开始新的任务。

例如,在下面的代码中;

如果run()ofWatchAgent时间超过 30 秒,是否会在完成之前创建一个新代理?

其次,如果我创建 的实例WatchAgent,我可以在每次定期运行时继续使用相同的实例吗?

0 投票
2 回答
2273 浏览

java - Java ScheduledExecutorService 精度差

您好,我编写了一个简单的程序来测试 ScduledExecutorService.schedule() 函数的精度。

测试设置延迟并检查所需延迟和有效结果之间的有效距离。

测试在运行 Linux 3.8 x86_64 的 i7 机器上执行,同时使用 OpenJDK 1.7 和 Oracle JDK 1.7

测试的结果真的很糟糕,这里有一个列表可以显示假定延迟和有效延迟之间的平均增量:


传奇:

  • Sleep(ms):所需的延迟时间(以毫秒为单位)
  • deltaAVG(ms):所需延迟和有效延迟之间的平均差异,以毫秒为单位
  • deltaAVG_PERC:所需/有效的错误百分比
  • delta MIN/MAX:获得的所需延迟和有效延迟之间的最小/最大差异

如您所见,称为 deltaAVG 的平均误差相对于延迟增加。

如何在延迟中获得更好的结果?我的意思是 i7 机器上 10 微秒的 300% 错误率太多了。

这是我用于测试的代码: