我正在尝试实现生产者 - 消费者模式,我希望能够阻止消费者。到目前为止我写道:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class Test {
public static void main(final String[] args) throws InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<>(100);
final ExecutorService producer = Executors.newSingleThreadExecutor();
final Consumer consumer = new Consumer(messages);
producer.execute(new Runnable() {
@Override
public void run() {
for (int i = 0;; i++) {
try {
messages.put("Message " + i);
Thread.sleep(500);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
});
consumer.start();
consumer.stop();
consumer.start();
}
private static class Consumer {
private final ExecutorService consumer = Executors.newSingleThreadExecutor();
private final BlockingQueue<String> messages;
public Consumer(final BlockingQueue<String> m) {
messages = m;
}
public void start() {
consumer.execute(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println(messages.take());
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
});
}
public void stop() {
consumer.shutdownNow();
}
}
}
但这总是给我上面的例外。控制台输出是:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task Test$Consumer$1@31602bbc rejected from java.util.concurrent.ThreadPoolExecutor@20d75cf7[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:628)
at Test$Consumer.start(Test.java:46)
at Test.main(Test.java:31)
为什么我无法重启&#34;遗嘱执行人或我如何实现这一目标?
提前致谢!
答案 0 :(得分:1)
答案 1 :(得分:0)
这是因为当您的班级Consumer
尝试consumer.execute(new Runnable() {
时,其执行人已被关闭。为什么?因为在行consumer.start();
之后,您的程序会立即终止:您不会等到任务执行完毕。
你应该修改你的代码:
submit
方法,而不是execute
:producer.submit(new Runnable() {...}
从execute()
方法Future<?>
对象接收:
Future f = producer.submit(new Runnable() { ... }
等到该对象终止:
consumer.start();
f.get();
更新根据有关的更新代码,我们应该讨论两个新行:
consumer.start();
consumer.stop();
consumer.start();
你应该知道,在执行者被停止一次后,你将无法在其中执行新任务。这就解释了为什么你在最后一行consumer.start();
收到此例外。