暂停 ScheduledExecutorService
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就可以做
一开始
。如果您使用的 ScheduledExecutorService 有一个队列,为什么要使用它来添加到另一个队列。不能只使用服务中的队列吗?
You can do
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?
当执行器关闭时,它不再接受新任务并等待当前任务终止。但您不想终止执行程序,只需暂停它即可。
所以你能做的就是在你的任务中你只处理一个空队列。因为你的任务只是偶尔执行,所以当没有处理可做时,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:
it would complicate your code.
what you need: blocking the task if
the queue is empty.
periodic task run and check if the
queue isEmpty.
other way in your task anyway
otherwise you would have
NullPointerException when the queue
is empty.