ExecutorService 不工作
ExecutorService doens't work
我在使用 Executorservice 时遇到问题
我实现了消费者-生产者模式
主要
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10000);
Thread producer = new Thread(new Producer(queue));
ExecutorService executorService = Executors.newFixedThreadPool(3);
Runnable consumer1 = new Consumer(queue);
Runnable consumer2 = new Consumer(queue);
Runnable consumer3 = new Consumer(queue);
producer.start();
executorService.submit(consumer1);
executorService.submit(consumer2);
executorService.submit(consumer3);
executorService.shutdown();
}
}
制作人
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable{
public BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10000);
public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
public synchronized void run() {
for (int i=0; i<100; ++i) {
try {
//System.out.println("i = " + i);
queue.put(i);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
消费者
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Consumer implements Runnable {
public BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10000);
public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
public void run() {
while (true) {
try {
//queue.take(); // case 1
System.out.println(Thread.currentThread().getName() + " Consumer : " + queue.take()); // case 2
} catch (InterruptedException e) {
System.out.println(e);
}
if (queue.isEmpty()) {
break;
}
}
}
}
我想知道为什么 (Consumer.java) 案例 1 不起作用,
但是情况 2 没问题
It print noting and never stopped(这个评论不好,无视ㅠㅠ)
我只是想知道,为什么情况 1 没有停止。
System.out.println 或 BlockingQueue 中有什么东西吗?
(Poducer.java 也。如果我在 Producer.java 中添加 print i 然后抛出 InterruptedException)
可能是我不了解java并且穿得很好。
请帮助我;(
(我的英语不好,抱歉)
这里的根本问题是,如果队列在 queue.take()
之前变空,消费者线程将阻塞,直到有内容添加到队列中。由于您在启动消费者之前将所有添加到队列中,因此其中一个消费者是否会进入阻塞状态是一个运气问题。
情况 2(带有控制台输出)似乎减慢了速度,以至于没有线程进入此状态。在情况 1 中,处理速度如此之快,以至于至少有一个线程发现自己被阻塞了。当我 运行 你的代码时,我发现线程 3 被阻塞,这意味着线程 1 和 2 可能在线程 3 有机会开始之前就消耗了队列中的所有条目。
如果您的用例涉及队列首先被 Producer
填充,然后 运行 Consumer
线程,您应该使用 poll()
而不是 take()
这会让你检测 运行 out of elements.
的条件
我在使用 Executorservice 时遇到问题
我实现了消费者-生产者模式
主要
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10000);
Thread producer = new Thread(new Producer(queue));
ExecutorService executorService = Executors.newFixedThreadPool(3);
Runnable consumer1 = new Consumer(queue);
Runnable consumer2 = new Consumer(queue);
Runnable consumer3 = new Consumer(queue);
producer.start();
executorService.submit(consumer1);
executorService.submit(consumer2);
executorService.submit(consumer3);
executorService.shutdown();
}
}
制作人
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable{
public BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10000);
public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
public synchronized void run() {
for (int i=0; i<100; ++i) {
try {
//System.out.println("i = " + i);
queue.put(i);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
消费者
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Consumer implements Runnable {
public BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10000);
public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
public void run() {
while (true) {
try {
//queue.take(); // case 1
System.out.println(Thread.currentThread().getName() + " Consumer : " + queue.take()); // case 2
} catch (InterruptedException e) {
System.out.println(e);
}
if (queue.isEmpty()) {
break;
}
}
}
}
我想知道为什么 (Consumer.java) 案例 1 不起作用, 但是情况 2 没问题
It print noting and never stopped(这个评论不好,无视ㅠㅠ)
我只是想知道,为什么情况 1 没有停止。
System.out.println 或 BlockingQueue 中有什么东西吗?
(Poducer.java 也。如果我在 Producer.java 中添加 print i 然后抛出 InterruptedException)
可能是我不了解java并且穿得很好。
请帮助我;( (我的英语不好,抱歉)
这里的根本问题是,如果队列在 queue.take()
之前变空,消费者线程将阻塞,直到有内容添加到队列中。由于您在启动消费者之前将所有添加到队列中,因此其中一个消费者是否会进入阻塞状态是一个运气问题。
情况 2(带有控制台输出)似乎减慢了速度,以至于没有线程进入此状态。在情况 1 中,处理速度如此之快,以至于至少有一个线程发现自己被阻塞了。当我 运行 你的代码时,我发现线程 3 被阻塞,这意味着线程 1 和 2 可能在线程 3 有机会开始之前就消耗了队列中的所有条目。
如果您的用例涉及队列首先被 Producer
填充,然后 运行 Consumer
线程,您应该使用 poll()
而不是 take()
这会让你检测 运行 out of elements.