暂停 ScheduledExecutorService

发布于 2024-11-18 04:06:31 字数 1159 浏览 3 评论 0原文

我正在使用 ScheduledExecutorService 来执行以固定速率调用服务的任务。该服务可能会向任务返回一些数据。该任务将数据存储在队列中。其他一些线程慢慢地从队列中选取项目

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class EverlastingThread implements Runnable {

    private ScheduledExecutorService executorService;
    private int time;
    private TimeUnit timeUnit;
    private BlockingQueue<String> queue = new LinkedBlockingQueue<String>(500);

    public EverlastingThread(ScheduledExecutorService executorService, int time, TimeUnit timeUnit) {
        this.executorService = executorService;
        this.time = time;
        this.timeUnit = timeUnit;
    }

    public void run() {

        // call the service. if Service returns any data put it an the queue
        queue.add("task");
    }

    public void callService() throws Exception {
        // while queue has stuff dont exucute???????????

        executorService.scheduleAtFixedRate(this, 0, time, timeUnit);
    }
}

如何暂停 executorService 直到任务填充的队列被清除。

I am using a ScheduledExecutorService to execute a task that calls a service at a fixed rate. The service may return some data to the task. The task stores data in a queue. Some other threads slowly pick items from the queue

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class EverlastingThread implements Runnable {

    private ScheduledExecutorService executorService;
    private int time;
    private TimeUnit timeUnit;
    private BlockingQueue<String> queue = new LinkedBlockingQueue<String>(500);

    public EverlastingThread(ScheduledExecutorService executorService, int time, TimeUnit timeUnit) {
        this.executorService = executorService;
        this.time = time;
        this.timeUnit = timeUnit;
    }

    public void run() {

        // call the service. if Service returns any data put it an the queue
        queue.add("task");
    }

    public void callService() throws Exception {
        // while queue has stuff dont exucute???????????

        executorService.scheduleAtFixedRate(this, 0, time, timeUnit);
    }
}

How do I pause the executorService until the queue populated by the task has been cleared.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2024-11-25 04:06:31

就可以做

if(!queue.isEmpty()) return; 

一开始

。如果您使用的 ScheduledExecutorService 有一个队列,为什么要使用它来添加到另一个队列。不能只使用服务中的队列吗?

You can do

if(!queue.isEmpty()) return; 

at the start.

If you are usin a ScheduledExecutorService which has a queue, why are you using it to add to another queue. Can you not just use the queue in the service?

花开半夏魅人心 2024-11-25 04:06:31

当执行器关闭时,它不再接受新任务并等待当前任务终止。但您不想终止执行程序,只需暂停它即可。

所以你能做的就是在你的任务中你只处理一个空队列。因为你的任务只是偶尔执行,所以当没有处理可做时,CPU 消耗将接近于 0。这是“if(!queue.isEmpty()) 返回;”来自彼得·劳瑞的回应。

其次,使用阻塞队列。这意味着,如果您在队列为空时调用 take() 方法来获取排队元素,则执行器线程将等待,直到某个元素自动添加到队列中。

所以:

  • 你不能暂停一个执行者
    这会让你的代码变得复杂。
  • 阻塞队列的设计正是如此
    你需要什么:阻止任务,如果
    队列是空的。
  • 如果你愿意,你可以让
    定期运行任务并检查是否
    队列为空。
  • 您应该已经使用过其中一个或
    无论如何,你的任务中还有其他方式
    否则你就会
    队列时出现 NullPointerException
    是空的。

When an executor is shudown, it do no longer accept new task and wait for the current ones to terminate. But you don't want to terminate your executor, just pause it.

So what you can do, is that in your task you just deal with an empty queue. Because you task is only to be executed from time to time, CPU consumption will be near to 0 for it when there is no processing to do. this is the "if(!queue.isEmpty()) return;" from Peter Lawrey response.

Second, you use a blocking queue. That mean that if you call the method take() to get a queued element while the queue is empty, the executor thread will wait until some element is added to the queue automatically.

So:

  • You can't pause an executor an even
    it would complicate your code.
  • A blocking queue do by design exactly
    what you need: blocking the task if
    the queue is empty.
  • if you prefer you can just let the
    periodic task run and check if the
    queue isEmpty.
  • You should already use one or the
    other way in your task anyway
    otherwise you would have
    NullPointerException when the queue
    is empty.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文