Tag: scheduledexecutorservice

scheduleAtFixedRate未在指定的延迟时间启动任务

我使用SchedulerExecuterService在指定的延迟和给定的时间间隔后执行任务。 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); scheduler.scheduleAtFixedRate(taskThread,60 ,120 ,TimeUnit.SECONDS); 发生的事情是第一次taskThread在延迟60秒后没有启动,它在延迟超过60秒后启动。 而接下来的执行以120秒的正确间隔开始(不完全是120秒,但是有一个非常小的延迟,可以忽略)。 我的查询是为什么第一次执行延迟超过60秒? 由于任务由线程执行,启动时间是否取决于线程优先级? 如何让它以60秒的精确延迟运行? Quartz库怎么样? 这个库能解决我的目的吗(在特定时间运行工作没有任何延迟)? 提前致谢。

如何正确使用ScheduledExecutorService?

所以这是我第一次使用ScheduledFuture而且我承认我可能已经在我的头上了。 我似乎无法让下面的示例工作。 目标只是采取两组动作,每组都有自己的超时,然后继续下一组并无限期地重复。 static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); ScheduledFuture warm = executor.scheduleWithFixedDelay(() -> { System.out.println(“warmbeans”); //do more stuff here }, 0, 1000, TimeUnit.MILLISECONDS); ScheduledFuture cool = executor.scheduleWithFixedDelay(() -> { System.out.println(“coolbeans”); //do more stuff here }, 0, 500, TimeUnit.MILLISECONDS); while(true) { try {warm.get(1000, TimeUnit.MILLISECONDS);} catch (InterruptedException | ExecutionException | TimeoutException e) {Listen.cancel(true);} try {cool.get(500, TimeUnit.MILLISECONDS);} catch […]

ScheduledExecutorService与使用Thread.sleep()滚动自己的Runnable之间的区别

使用ScheduledExecutorService的scheduleAtFixedRate()运行一段代码而不是创建一个新的Runnable有一个好处,它有一个永久循环与Thread.sleep() ,导致线程为所需的内容hibernate期? 使用其中一种方法可以获得性能提升吗?

ExecutorService不在JUnit测试中执行Runnables

我有一个我想测试的执行器服务。 我有几个需要调用的任务(Runnables),然后调用wait()并且几秒钟之后虚拟计时器调用notify() 。 我这样做是为了检查所有任务是否按顺序执行并在一段时间后执行。 问题是我的Runnable的run()方法根本没有被调用。 我设置了一个断点,代码执行没有达到它。 在普通编码中,它运作良好。 任务排队,他们调用wait()直到没有获得某些响应或触发超时,他们调用notify()并执行下一个任务。 问题是我运行测试时。 有什么想法吗?

Thread.sleep()VS Executor.scheduleWithFixedDelay()

目标:每隔一段时间执行一次代码。 问题:在性能方面,是否存在显着差异: while(true) { execute(); Thread.sleep(10 * 1000); } 和 executor.scheduleWithFixedDelay(runnableWithoutSleep, 0, 10, TimeUnit.SECONDS); ? 当然,后一种选择更犹豫不决。 然而,我想知道我是否应该开始一项名为“花几天时间重构遗留代码以告别Thread.sleep()”的冒险。 更新:此代码在超级/超级/超高负载环境中运行。

如何安排任务运行一次?

我想延迟做一些事情,就像设置倒计时器一样,在一段时间后“做一件事”。 我希望程序的其余部分在我等待时继续运行,所以我尝试制作自己的Thread ,其中包含一分钟的延迟: public class Scratch { private static boolean outOfTime = false; public static void main(String[] args) { Thread countdown = new Thread() { @Override public void run() { try { // wait a while System.out.println(“Starting one-minute countdown now…”); Thread.sleep(60 * 1000); // do the thing outOfTime = true; System.out.println(“Out of time!”); } catch […]

使用servlet在Java EE环境中启动和停止ScheduledExecutorService

我们需要使用简单的servlet应用程序通过JMX监视远程JVM详细信息。 所以在一个独立的应用程序中完成的工作是 1)创建一个JMX连接器并获取内存数据 – >完成2)我们需要不断监视和获取记录(2.1>可以被视为计划任务的恒定延迟并将记录插入DB或2.2>是否JMX给出了历史记录,如果是,哪个MBean可以访问该信息?)。 在这里,我打算使用一个界面来注册域,然后就可以了。 有来自JSP的启动和停止按钮。 我们点击启动系统时的function将运行调度程序(ScheduledExecutorService)并在后台捕获记录以提供历史记录。 当使用点击停止时,调度程序必须停止后台进程。 问题是我们如何控制和获取调度程序的对象? 1)换句话说,我们如何通过servlet启动和停止ScheduledExecutorService? 从一个servlet启动一个线程并从一个特定任务的另一个servlet中停止一个线程? 2)如果我们有集群/负载平衡环境怎么办? 目前我正在考虑将每个ScheduledExecutorService添加到HashMap中,关键是任务对象和值是使用SingleTon设计模式的ScheduledExecutorService。 有没有默认方法。 使用SingleTon的整个循环处于集群/负载平衡环境中,我们可能无法获得适当的更新对象。 期待您的宝贵建议。