对于生产者消费者模型,如果我们采用常规的容器,那么我们需要同步,并且需要在生产者线程和消费者线程之间做wait和notify的线程间通信。而阻塞队列将同步操作和生产者和消费者间的通信在容器内部实现,使用起来就不需要我们自己做同步和线程通信了。
Java中提供的BlockingQueue包括:
ArrayBlockingQueue、LinkedBlockingQueue、PriorityBlockingQueue。
一个典型的应用场景就是socket客户端数据的读取和解析。
另外关于生产者-消费者模型,下面是采用Object.wait(),Object.notify()非阻塞容器实现的生产者-消费者模式:
摘抄自此
public class Test {
private int queueSize = 10;
private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);
public static void main(String[] args) {
Test test = new Test();
Producer producer = test.new Producer();
Consumer consumer = test.new Consumer();
producer.start();
consumer.start();
}
class Consumer extends Thread{
@Override
public void run() {
consume();
}
private void consume() {
while(true){
synchronized (queue) {
while(queue.size() == 0){
try {
System.out.println("队列空,等待数据");
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
queue.notify();
}
}
queue.poll(); //每次移走队首元素
queue.notify();
System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");
}
}
}
}
class Producer extends Thread{
@Override
public void run() {
produce();
}
private void produce() {
while(true){
synchronized (queue) {
while(queue.size() == queueSize){
try {
System.out.println("队列满,等待有空余空间");
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
queue.notify();
}
}
queue.offer(1); //每次插入一个元素
queue.notify();
System.out.println("向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size()));
}
}
}
}
}