Java并发机制(6)--阻塞队列
Java并发编程:阻塞队列
整理自:博客园-海子-http://www.cnblogs.com/dolphin0520/p/3933404.html
1、什么是阻塞队列
除了同步容器(Hashtable、Vector)、并发容器(ConcurrentHashMap、CopyOnWriteArrayList)外,还有一种容器就是阻塞队列;
阻塞队列会在一定条件下对当前线程产生阻塞,条件消失后唤醒被阻塞的线程。
2、几种主要的阻塞队列
ArrayBlockingQueue:基于数组实现,有界,先进先出
LinkedBlockingQueue:基于链表实现,有界,先进先出
PriorityBlockingQueue:基于优先级的,无解,优先级高的先出;
DelayQueue:延时阻塞,无界,指定一定延时时间
3、非阻塞队列与阻塞队列的方法比较:
3.1非阻塞队列(如deque,LinkedList,PriorityQueue等)
add(E e):加入队列末尾,成功返回true,失败抛出异常;
remove():移除并获取队首元素,为null则抛异常。
offer(E e):成功返回true,失败返回false,好于add因为它不会只抛一个异常;
poll():移除并获取队首元素,为空返回null,也好于remove;
peek():获取队首元素,失败返回null;
建议使用offer,poll,和peek。
3.2阻塞队列:
上述五种,且实现同步;
put(E e):存入队尾,满则等待;
take():取队首,队列空则等待;
offer(E e,long timeout, TimeUnit unit):队尾存,满则等待时间,还不行返回false;
poll(long timeout, TimeUnit unit):取队首,队列空则等待,时间到达后,返回元素,取不到返回null;
4、阻塞队列的实现原理:
使用可重入锁ReentrantLock,调用其可中断加锁方法lockInterruptibly()
5、java实例:
//非阻塞队列实现生产者消费者模式
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()));
}
}
}
}
}
//阻塞队列实现生产者消费者模式
public class Test {
private int queueSize = 10;
private ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<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){
try {
queue.take();
System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer extends Thread{
@Override
public void run() {
produce();
}
private void produce() {
while(true){
try {
queue.put(1);
System.out.println("向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

浙公网安备 33010602011771号