阻塞队列BlockingQueue
2015-01-18 18:38
239 查看
例介绍一个特殊的队列:BlockingQueue,如果BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒,同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间时才会被唤醒继续操作。
本例再次实现前面介绍的篮子程序,不过这个篮子中最多能放得苹果数不是1,可以随意指定。当篮子满时,生产者进入等待状态,当篮子空时,消费者等待。
BlockingQueue定义的常用方法如下:
add(anObject):把anObject加到BlockingQueue里,如果BlockingQueue可以容纳,则返回true,否则抛出异常。
offer(anObject):表示如果可能的话,将anObject加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则返回false。
put(anObject):把anObject加到BlockingQueue里,如果BlockingQueue没有空间,则调用此方法的线程被阻断直到BlockingQueue里有空间再继续。
poll(time):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回null。
take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到BlockingQueue有新的对象被加入为止。
BlockingQueue有四个具体的实现类,根据不同需求,选择不同的实现类:
ArrayBlockingQueue:规定大小的BlockingQueue,其构造函数必须带一个int参数来指明其大小。其所含的对象是以FIFO(先入先出)顺序排序的。
LinkedBlockingQueue:大小不定的BlockingQueue,若其构造函数带一个规定大小的参数,生成的BlockingQueue有大小限制,若不带大小参数,所生成的BlockingQueue的大小由Integer.MAX_VALUE来决定。其所含的对象是以FIFO顺序排序的。
PriorityBlockingQueue:类似于LinkedBlockingQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数所带的Comparator决定的顺序。
SynchronousQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成的。
LinkedBlockingQueue和ArrayBlockingQueue比较起来,它们背后所用的数据结构不一样,导致LinkedBlockingQueue的数据吞吐量要大于ArrayBlockingQueue,但在线程数量很大时其性能的可预见性低于ArrayBlockingQueue。
本例再次实现前面介绍的篮子程序,不过这个篮子中最多能放得苹果数不是1,可以随意指定。当篮子满时,生产者进入等待状态,当篮子空时,消费者等待。
BlockingQueue定义的常用方法如下:
add(anObject):把anObject加到BlockingQueue里,如果BlockingQueue可以容纳,则返回true,否则抛出异常。
offer(anObject):表示如果可能的话,将anObject加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则返回false。
put(anObject):把anObject加到BlockingQueue里,如果BlockingQueue没有空间,则调用此方法的线程被阻断直到BlockingQueue里有空间再继续。
poll(time):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回null。
take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到BlockingQueue有新的对象被加入为止。
BlockingQueue有四个具体的实现类,根据不同需求,选择不同的实现类:
ArrayBlockingQueue:规定大小的BlockingQueue,其构造函数必须带一个int参数来指明其大小。其所含的对象是以FIFO(先入先出)顺序排序的。
LinkedBlockingQueue:大小不定的BlockingQueue,若其构造函数带一个规定大小的参数,生成的BlockingQueue有大小限制,若不带大小参数,所生成的BlockingQueue的大小由Integer.MAX_VALUE来决定。其所含的对象是以FIFO顺序排序的。
PriorityBlockingQueue:类似于LinkedBlockingQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数所带的Comparator决定的顺序。
SynchronousQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成的。
LinkedBlockingQueue和ArrayBlockingQueue比较起来,它们背后所用的数据结构不一样,导致LinkedBlockingQueue的数据吞吐量要大于ArrayBlockingQueue,但在线程数量很大时其性能的可预见性低于ArrayBlockingQueue。
public class BlockingQueueTest { public static void main(String[] args) { Data da =new Data(); QueueThread th1 =new QueueThread(da); QueueThread2 th2 =new QueueThread2(da); ExecutorService pool =Executors.newFixedThreadPool(2); pool.execute(th1); pool.execute(th2); } } class Data{ final BlockingQueue queue = new ArrayBlockingQueue(3); public void setData(){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"准备放入数据>>"); try { queue.put(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "已经放了数据," + "队列目前有" + queue.size() + "个数据"); } public void getData(){ try{ Thread.sleep(300); }catch(Exception e){ e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"准备获得数据.."); try { queue.take(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"获取数据完毕队列目前有" + queue.size() + "个数据"); } } class QueueThread implements Runnable{ private Data da; public QueueThread(Data da) { this.da =da; } @Override public void run() { for(int i=0;i<20;i++){ da.setData(); } } } class QueueThread2 implements Runnable{ private Data da; public QueueThread2(Data da) { this.da =da; } @Override public void run() { for(int i=0;i<20;i++){ da.getData(); } } }
pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-1已经放了数据,队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-1已经放了数据,队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-1已经放了数据,队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-1已经放了数据,队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据 pool-1-thread-2准备获得数据.. pool-1-thread-1准备放入数据>> pool-1-thread-1已经放了数据,队列目前有1个数据 pool-1-thread-2获取数据完毕队列目前有0个数据
相关文章推荐
- Spring MVC 基于阻塞队列 LinkedBlockingQueue 的同步长轮询功能实现
- (13)多线程与并发库之java5阻塞队列(BlockingQueue)的应用----子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次
- (java多线程与并发)java并发库中的阻塞队列--BlockingQueue
- Java线程(篇外篇):阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- JAVA并发之阻塞队列LinkedBlockingQueue与ArrayBlockingQueue
- Windows下一个并发阻塞队列(BlockingQueue)
- 【Java并发】阻塞队列BlockingQueue和生产者-消费者模型笔记
- 阻塞队列之LinkedBlockingQueue
- Java并发编程:阻塞队列BlockingQueue
- Java线程(十三):BlockingQueue-线程的阻塞队列
- 移动端并发编程基础篇-阻塞队列ArrayBlockingQueue&LinkedBlockingQueue
- JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue
- JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue
- BlockingQueue(阻塞队列)详解(一个生产者消费者的实例)
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
- Windows下一个并发阻塞队列(BlockingQueue)
- 阻塞队列--LinkedBlockingQueue
- Java并发包:阻塞队列(BlockingQueue)