我正在研究BlockingQueue接口,其中ArrayBlockingQueue是一个实现。为了演示目的,我开发了以下代码:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class MainJava {
public static void main(String[] args) {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
new Thread(producer).start();
new Thread(consumer).start();
}
}
class Producer implements Runnable {
BlockingQueue<String> queue = null;
public Producer(BlockingQueue<String> queue) {
this.queue = queue;
// TODO Auto-generated constructor stub
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("Producer added " + i);
queue.put(String.valueOf(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
BlockingQueue<String> queue = null;
public Consumer(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("Consumer used " + queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
预期行为
Producer add 0
Consumer used 0
Producer add 1
Consumer used 1
and so on..
Actaul O / P
Producer added 0
Producer added 1
Consumer used 0
Consumer used 1
Producer added 2
Producer added 3
Producer added 4
我尝试使用调试模式,它正常工作。那么为什么没有调试模式,事情就不能正常工作?
答案 0 :(得分:2)
您的代码可以正常运行 - 它只是一种幻觉&#39;您的队列似乎一次包含多个项目,这是由不幸的时间导致的,而System.out.println
位于错误的位置:
在您的制作人中,System.out.println
位于阻止put
之前,因此您正在打印&#39;制作人添加了x&#39;即使put
必须等到队列变空。
但即使我们交换这两行(put
之前的System.out.println
),输出似乎也是错误的:
Consumer used 0
Producer added 0
Producer added 1
Consumer used 1
Consumer used 2
Producer added 2
Producer added 3
Consumer used 3
然而,这是一个由不幸的时机引起的错觉。正如您所看到的,在我的输出中,消费者似乎在生产者将其放入队列之前消耗元素0 !当然不是这种情况。 System.out
是PrintStream
对象,它是线程安全的。因此,只有一个线程可以同时打印某些内容。在上面的运行中,消费者线程只是在生产者之前抓住了锁。
执行顺序可能是这样的:
put 0 into queue
take 0 from queue
print 'Consumer used 0'
wait until queue not empty
print 'Producer added 0'
put 1 into queue
print 'Producer added 1'
wait until queue not full
take 1 from queue
print 'Consumer used 1'
put 2 into queue
take 2 from queue
print 'Consumer used 2'
print 'Producer added 2'
等
多线程系统很难争论正确的行为......
答案 1 :(得分:2)
@Kayaman实际上已经回答了这个问题,但也许在这里可能会提供更多信息。
编辑...和isnot2bad写了类似的答案(+1)。我显然太慢了。但是,也许它被认为是有用的。
首先:代码
System.out.println("Producer added " + i);
queue.put(String.valueOf(i));
显然会在实际添加到队列之前打印Producer added 1
。只需将线路交换到
queue.put(String.valueOf(i));
System.out.println("Producer added " + i);
将减少输出似乎错误的情况,但不会完全消除它们。原因是这两行不是原子。
显示每个线程在每个步骤中执行的操作的示例计划表明结果可能看起来是任意错误的,具体取决于操作的顺序:
Producer thread: Consumer thread: Queue: Output:
------------------------------------------------------------------------
queue.put(0); [0]
println("Added " + 0); [] Added 0
String 0 = queue.take(); []
println("Used " + 0); [] Used 0
queue.put(1); [1]
String 1 = queue.take(); []
println("Added " + 1); [] Added 1
queue.put(2); [2]
println("Added " + 2); [2] Added 2
println("Used " + 1); [2] Used 1
String 2 = queue.take(); []
println("Used " + 2); [] Used 2
queue.put(3); [3]
String 3 = queue.take(); []
println("Used " + 3); [] Used 3
println("Added " + 3); [] Added 3
queue.put(4); [4]
println("Added " + 4); [4] Added 4
但关键是队列永远不会包含多个元素。