【发布时间】:2016-03-12 06:34:50
【问题描述】:
我在使用 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很好
它打印出注意并且从不停止(这个评论不好。忽略它ㅠㅠ)
我只是想知道,为什么案例 1 不是停止。
System.out.println 或 BlockingQueue 中有什么东西吗?
(Poducer.java 也是。如果我在 Producer.java 中添加了 print i 则抛出 InterruptedException)
可能是我不太了解java和thread。
请帮帮我;( (我的英语不好,对不起)
【问题讨论】:
-
“不工作”是什么意思?你期望会发生什么,相反会发生什么?为什么调用
queue.take()会打印任何内容? -
对不起,我的英语不好。我的意思是如果我添加 queue.take() (不使用 System.out.println)这个过程永远不会停止。不是我修复了 queue.empty() 检查,while 循环中的第一行。
-
不是 (x) 现在 (o) 我解决了这个问题。谢谢@JBNizet 我应该学会如何提问。
标签: java multithreading executorservice blockingqueue