博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA并发,BlockingQuene
阅读量:7211 次
发布时间:2019-06-29

本文共 2990 字,大约阅读时间需要 9 分钟。

BlockingQueue也是java.util.concurrent下的主要用来控制线程同步的工具。

BlockingQueue有四个具体的实现类,根据不同需求,选择不同的实现类

1、ArrayBlockingQueue:一个由数组支持的有界阻塞队列,规定大小的BlockingQueue,其构造函数必须带一个int参数来指明其大小.其所含的对象是以FIFO(先入先出)顺序排序的。

2、LinkedBlockingQueue:大小不定的BlockingQueue,若其构造函数带一个规定大小的参数,生成的BlockingQueue有大小限制,若不带大小参数,所生成的BlockingQueue的大小由Integer.MAX_VALUE来决定.其所含的对象是以FIFO(先入先出)顺序排序的。

3、PriorityBlockingQueue:类似于LinkedBlockQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数的Comparator决定的顺序。

4、SynchronousQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成的。

 

LinkedBlockingQueue 可以指定容量,也可以不指定,不指定的话,默认最大是Integer.MAX_VALUE,其中主要用到put和take方法,put方法在队列满的时候会阻塞直到有队列成员被消费,take方法在队列空的时候会阻塞,直到有队列成员被放进来。

 

生产者消费者的示例代码:

1 package com.xt.thinks21_7; 2  3 import java.util.concurrent.BlockingQueue; 4 import java.util.concurrent.ExecutorService; 5 import java.util.concurrent.Executors; 6 import java.util.concurrent.LinkedBlockingDeque; 7  8 /** 9  * 使用BlockingQuene模拟生产者与消费者10  * 11  * @author Ymmmsick12  *13  */14 public class BlockingQueneTest {15 16     public static void main(String[] args) {17         BlockingQueue
quene = new LinkedBlockingDeque
(2);18 ExecutorService es = Executors.newCachedThreadPool();19 for (int i = 0; i < 10; i++) {20 es.execute(new Product(quene, "Thread->" + i));21 es.execute(new Consumer(quene));22 }23 24 es.shutdown();25 }26 }27 28 class Product implements Runnable {29 30 private BlockingQueue
quene;31 private String name;32 33 public Product(BlockingQueue
quene, String name) {34 this.quene = quene;35 this.name = name;36 }37 38 @Override39 public void run() {40 // TODO Auto-generated method stub41 try {42 quene.put(name);43 System.out.println("Product :" + name);44 } catch (InterruptedException e) {45 // TODO Auto-generated catch block46 e.printStackTrace();47 }48 }49 50 }51 52 class Consumer implements Runnable {53 54 private BlockingQueue
quene;55 56 public Consumer(BlockingQueue
quene) {57 this.quene = quene;58 }59 60 @Override61 public void run() {62 // TODO Auto-generated method stub63 try {64 String t = quene.take();65 System.out.println("Consumer:" + t);66 } catch (InterruptedException e) {67 // TODO Auto-generated catch block68 e.printStackTrace();69 }70 }71 72 }

输出结果:

Product :Thread->0

Consumer:Thread->0

Product :Thread->1

Product :Thread->2

Consumer:Thread->1

Consumer:Thread->2

Product :Thread->3

Consumer:Thread->3

Product :Thread->4

Consumer:Thread->4

Product :Thread->5

Consumer:Thread->5

Product :Thread->6

Consumer:Thread->6

Product :Thread->7

Consumer:Thread->7

Product :Thread->8

Consumer:Thread->8

Product :Thread->9

Consumer:Thread->9

 

结论:LinkedBlockingQuene在同一时间段内最多只能保持两个对象在队列,对象溢满的时候生产者会等待阻塞,对象空置的时候消费者会等待阻塞。

转载于:https://www.cnblogs.com/wubingshenyin/p/4496002.html

你可能感兴趣的文章
Ajax-jQuery_Ajax_实例 ($.ajax、$.post、$.get)
查看>>
Python实现web动态服务器
查看>>
新客户上云 –虚拟机及相关服务常见问题集锦
查看>>
IntelliJ Idea 常用快捷键列表
查看>>
各数据库连接配置与maven依赖安装
查看>>
Linux(centOS)手动安装删除Apache+MySQL+PHP+Memcached原创无错版
查看>>
Nginx的启动(start),停止(stop)命令
查看>>
代码生成工具更新--快速生成Winform框架的界面项目
查看>>
Jquery根据JSON生成Table
查看>>
[Oracle]Sqlplus 中使用 new_value
查看>>
【HTTP】 认证和单点登录 【瞎写的…】
查看>>
微信小程序-上传多张图片加进度条(支持预览、删除)
查看>>
Java基础-SSM之mybatis快速入门篇
查看>>
error C2220: 警告被视为错误 - 没有生成“object”文件
查看>>
IO is frozen on database xxx, No user action is required
查看>>
执行perl xttdriver.pl报错Can't locate Getopt/Long.pm in @INC
查看>>
log4j的最佳实践(转)
查看>>
linux中 jdk 的卸载和安装[转]
查看>>
Install And Configure ColdFusion MX 6.1 on Windows
查看>>
[转]Error: "SQL BPA command line has encountered a problem and needs to close"
查看>>