演示一个阻塞队列的使用

 public class BlockingQueueTest
{
public static void main(String[] args)
{
//创建一个包含三个元素的阻塞队列
final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
for (int i = 0; i < 2; i++)
{
new Thread(new Runnable()
{
@Override
public void run()
{
while(true){
try
{
Thread.sleep(1000);
System.out.println("线程"+Thread.currentThread().getName()+" 准备放数据了");
queue.put(new Random().nextInt(100));
System.out.println("线程"+Thread.currentThread().getName()+" 已经放完数据了,目前队列有"+queue.size()+"个数据");
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start(); } new Thread(new Runnable()
{
@Override
public void run()
{
while(true){
try
{
//将此处的睡眠时间改为200和2000 分别观察结果
Thread.sleep(2000);
System.out.println("线程"+Thread.currentThread().getName()+" 准备取数据了");
queue.take();
System.out.println("线程"+Thread.currentThread().getName()+" 已经取完数据了,目前队列有"+queue.size()+"个数据");
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
}
}

部分运行结果如下,

线程Thread-1 准备放数据了
线程Thread-0 准备放数据了
线程Thread-1 已经放完数据了,目前队列有1个数据
线程Thread-0 已经放完数据了,目前队列有2个数据
线程Thread-2 准备取数据了
线程Thread-2 已经取完数据了,目前队列有1个数据
线程Thread-1 准备放数据了
线程Thread-0 准备放数据了
线程Thread-1 已经放完数据了,目前队列有2个数据
线程Thread-0 已经放完数据了,目前队列有3个数据
线程Thread-1 准备放数据了
线程Thread-0 准备放数据了
线程Thread-2 准备取数据了
线程Thread-2 已经取完数据了,目前队列有2个数据
线程Thread-1 已经放完数据了,目前队列有3个数据
线程Thread-1 准备放数据了
线程Thread-2 准备取数据了
线程Thread-2 已经取完数据了,目前队列有2个数据
线程Thread-0 已经放完数据了,目前队列有3个数据
线程Thread-0 准备放数据了
线程Thread-2 准备取数据了
线程Thread-2 已经取完数据了,目前队列有2个数据
线程Thread-1 已经放完数据了,目前队列有3个数据
线程Thread-1 准备放数据了
线程Thread-2 准备取数据了
线程Thread-2 已经取完数据了,目前队列有2个数据
线程Thread-0 已经放完数据了,目前队列有3个数据
线程Thread-0 准备放数据了

* 面试题
* 需求:
* 子线程循环输出10次
* 接着主线程循环输出100次
* 接着又回到子线程执行10次
* 再回到主线程执行100次
* 如此往复50次 请写出程序
* 此处使用阻塞队列来实现

 public class BlockingQueueCommunication
{
public static void main(String[] args) throws InterruptedException
{
final Core core = new Core();
//子线程
new Thread(
new Runnable()
{
@Override
public void run()
{
for (int i = 1; i <= 50;i++)
{
core.SubMethod(i);
}
}
}
).start(); //主线程
for (int i = 1; i <= 50; i++)
{
core.MainMethod(i);
}
} /**
* 创建一个静态的类
* 将核心的业务逻辑的方法放在这里
* 体现了高内聚的特点
* @author huang.jf
*
*/
static class Core{
//创建两个阻塞队列 都只能装一个元素
//实现的效果就是q1存,q2取,q2存,q1取
//用两个具有一个空间的队列来实现同步通知的功能
private BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
private BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1); //匿名构造方法 也叫非静态代码块
//当创建对象时 该代码块先被执行
//保证初始化,在queue2队列中包含一个元素
{
try
{
queue2.put(1);
} catch (InterruptedException e)
{
e.printStackTrace();
}
} //子线程调用 循环输出10次
public void SubMethod(int j){
try
{
queue1.put(1);//向q1中存入一个值
} catch (InterruptedException e)
{
e.printStackTrace();
}
for (int i = 1; i <= 10; i++)
{
System.out.println("this is sub thread..."+i+"......."+j);
}
try
{
queue2.take();//取出q2中的一个值
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
//主线程调用循环输出一百次
public void MainMethod(int j){
try
{
queue2.put(1);//一开始queue2队列会被阻塞,因为初始化的时候已经有一个值
} catch (InterruptedException e)
{
e.printStackTrace();
}
for (int i = 1; i <= 100; i++)
{
System.out.println("this is main thread..."+i+"......"+j);
}
try
{
queue1.take();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

部分运行结果如下:

this is sub thread...1.......49
this is sub thread...2.......49
this is sub thread...3.......49
this is sub thread...4.......49
this is sub thread...5.......49
this is sub thread...6.......49
this is sub thread...7.......49
this is sub thread...8.......49
this is sub thread...9.......49
this is sub thread...10.......49
this is main thread...1......49
this is main thread...2......49
this is main thread...3......49
this is main thread...4......49
this is main thread...5......49
this is main thread...6......49
this is main thread...7......49
this is main thread...8......49
this is main thread...9......49
this is main thread...10......49
this is main thread...11......49
this is main thread...12......49
this is main thread...13......49
this is main thread...14......49
this is main thread...15......49
this is main thread...16......49
this is main thread...17......49
this is main thread...18......49
this is main thread...19......49
this is main thread...20......49
this is main thread...21......49
this is main thread...22......49
this is main thread...23......49
this is main thread...24......49
this is main thread...25......49
this is main thread...26......49
this is main thread...27......49
this is main thread...28......49
this is main thread...29......49
this is main thread...30......49
this is main thread...31......49
this is main thread...32......49
this is main thread...33......49
this is main thread...34......49
this is main thread...35......49
this is main thread...36......49
this is main thread...37......49
this is main thread...38......49
this is main thread...39......49
this is main thread...40......49
this is main thread...41......49
this is main thread...42......49
this is main thread...43......49
this is main thread...44......49
this is main thread...45......49
this is main thread...46......49
this is main thread...47......49
this is main thread...48......49
this is main thread...49......49
this is main thread...50......49
this is main thread...51......49
this is main thread...52......49
this is main thread...53......49
this is main thread...54......49
this is main thread...55......49
this is main thread...56......49
this is main thread...57......49
this is main thread...58......49
this is main thread...59......49
this is main thread...60......49
this is main thread...61......49
this is main thread...62......49
this is main thread...63......49
this is main thread...64......49
this is main thread...65......49
this is main thread...66......49
this is main thread...67......49
this is main thread...68......49
this is main thread...69......49
this is main thread...70......49
this is main thread...71......49
this is main thread...72......49
this is main thread...73......49
this is main thread...74......49
this is main thread...75......49
this is main thread...76......49
this is main thread...77......49
this is main thread...78......49
this is main thread...79......49
this is main thread...80......49
this is main thread...81......49
this is main thread...82......49
this is main thread...83......49
this is main thread...84......49
this is main thread...85......49
this is main thread...86......49
this is main thread...87......49
this is main thread...88......49
this is main thread...89......49
this is main thread...90......49
this is main thread...91......49
this is main thread...92......49
this is main thread...93......49
this is main thread...94......49
this is main thread...95......49
this is main thread...96......49
this is main thread...97......49
this is main thread...98......49
this is main thread...99......49
this is main thread...100......49
this is sub thread...1.......50
this is sub thread...2.......50
this is sub thread...3.......50
this is sub thread...4.......50
this is sub thread...5.......50
this is sub thread...6.......50
this is sub thread...7.......50
this is sub thread...8.......50
this is sub thread...9.......50
this is sub thread...10.......50
this is main thread...1......50
this is main thread...2......50
this is main thread...3......50
this is main thread...4......50
this is main thread...5......50
this is main thread...6......50
this is main thread...7......50
this is main thread...8......50
this is main thread...9......50
this is main thread...10......50
this is main thread...11......50
this is main thread...12......50
this is main thread...13......50
this is main thread...14......50
this is main thread...15......50
this is main thread...16......50
this is main thread...17......50
this is main thread...18......50
this is main thread...19......50
this is main thread...20......50
this is main thread...21......50
this is main thread...22......50
this is main thread...23......50
this is main thread...24......50
this is main thread...25......50
this is main thread...26......50
this is main thread...27......50
this is main thread...28......50
this is main thread...29......50
this is main thread...30......50
this is main thread...31......50
this is main thread...32......50
this is main thread...33......50
this is main thread...34......50
this is main thread...35......50
this is main thread...36......50
this is main thread...37......50
this is main thread...38......50
this is main thread...39......50
this is main thread...40......50
this is main thread...41......50
this is main thread...42......50
this is main thread...43......50
this is main thread...44......50
this is main thread...45......50
this is main thread...46......50
this is main thread...47......50
this is main thread...48......50
this is main thread...49......50
this is main thread...50......50
this is main thread...51......50
this is main thread...52......50
this is main thread...53......50
this is main thread...54......50
this is main thread...55......50
this is main thread...56......50
this is main thread...57......50
this is main thread...58......50
this is main thread...59......50
this is main thread...60......50
this is main thread...61......50
this is main thread...62......50
this is main thread...63......50
this is main thread...64......50
this is main thread...65......50
this is main thread...66......50
this is main thread...67......50
this is main thread...68......50
this is main thread...69......50
this is main thread...70......50
this is main thread...71......50
this is main thread...72......50
this is main thread...73......50
this is main thread...74......50
this is main thread...75......50
this is main thread...76......50
this is main thread...77......50
this is main thread...78......50
this is main thread...79......50
this is main thread...80......50
this is main thread...81......50
this is main thread...82......50
this is main thread...83......50
this is main thread...84......50
this is main thread...85......50
this is main thread...86......50
this is main thread...87......50
this is main thread...88......50
this is main thread...89......50
this is main thread...90......50
this is main thread...91......50
this is main thread...92......50
this is main thread...93......50
this is main thread...94......50
this is main thread...95......50
this is main thread...96......50
this is main thread...97......50
this is main thread...98......50
this is main thread...99......50
this is main thread...100......50

java 5并发中的阻塞队列ArrayBlockingQueue的使用以及案例实现的更多相关文章

  1. Java中的阻塞队列-ArrayBlockingQueue(一)

    最近在看一些java基础的东西,看到了队列这章,打算对复习的一些知识点做一个笔记,也算是对自己思路的一个整理,本章先聊聊java中的阻塞队列 参考文章: http://ifeve.com/java-b ...

  2. Java核心知识点学习----多线程中的阻塞队列,ArrayBlockingQueue介绍

    1.什么是阻塞队列? 所谓队列,遵循的是先进先出原则(FIFO),阻塞队列,即是数据共享时,A在写数据时,B想读同一数据,那么就将发生阻塞了. 看一下线程的四种状态,首先是新创建一个线程,然后,通过s ...

  3. 聊聊并发(七)——Java中的阻塞队列

    3. 阻塞队列的实现原理 聊聊并发(七)--Java中的阻塞队列 作者 方腾飞 发布于 2013年12月18日 | ArchSummit全球架构师峰会(北京站)2016年12月02-03日举办,了解更 ...

  4. java并发编程学习: 阻塞队列 使用 及 实现原理

    队列(Queue)与栈(Stack)是数据结构中的二种常用结构,队列的特点是先进先出(First In First Out),而Stack是先进后出(First In Last Out),说得通俗点: ...

  5. Java并发编程:阻塞队列(转载)

    Java并发编程:阻塞队列 在前面几篇文章中,我们讨论了同步容器(Hashtable.Vector),也讨论了并发容器(ConcurrentHashMap.CopyOnWriteArrayList), ...

  6. 【转】Java并发编程:阻塞队列

    在前面几篇文章中,我们讨论了同步容器(Hashtable.Vector),也讨论了并发容器(ConcurrentHashMap.CopyOnWriteArrayList),这些工具都为我们编写多线程程 ...

  7. 12、Java并发编程:阻塞队列

    Java并发编程:阻塞队列 在前面几篇文章中,我们讨论了同步容器(Hashtable.Vector),也讨论了并发容器(ConcurrentHashMap.CopyOnWriteArrayList), ...

  8. Java中的阻塞队列(BlockingQueue)

    1. 什么是阻塞队列 阻塞队列(BlockingQueue)是 Java 5 并发新特性中的内容,阻塞队列的接口是 java.util.concurrent.BlockingQueue,它提供了两个附 ...

  9. 多线程编程学习六(Java 中的阻塞队列).

    介绍 阻塞队列(BlockingQueue)是指当队列满时,队列会阻塞插入元素的线程,直到队列不满:当队列空时,队列会阻塞获得元素的线程,直到队列变非空.阻塞队列就是生产者用来存放元素.消费者用来获取 ...

随机推荐

  1. Docker 小记 — Compose & Swarm

    前言 任何相对完整的应用服务都不可能是由单一的程序来完成支持,计划使用 Docker 来部署的服务更是如此.大型服务需要进行拆分,形成微服务集群方能增强其稳定性和可维护性.本篇随笔将对 Docker ...

  2. 第二篇:数据可视化 - 基本API

    前言 数据可视化是数据挖掘非常重要的一个环节,它不单在查阅了解数据环节使用到,在整个数据挖掘的流程中都会使用到. 因为数据可视化不单可以形象地展示数据,让你对数据有更好的总体上的了解,而且还可以让你清 ...

  3. 安卓中webview读取html,同时嵌入Flex的SWF,交互

    安卓中webview读取html,同时嵌入Flex的SWF,交互 安卓activity与html交互很简单,用javascript接口即可,网上一堆的例子,基本上没多大问题. 在html里面嵌入swf ...

  4. 可重复执行的SQL Script

    问题 在工作中偶尔会遇到这样的问题:SQL script重复执行时会报错. 理想的状态下,SQL script跑一遍就够了,是不会重复执行的,但是实际情况往往很复杂. 比如Dev同学在开发时在A环境把 ...

  5. 【BZOJ3262】陌上花开(树套树)

    [BZOJ3262]陌上花开(树套树) 题面 对于权限题,我这种苦逼肯定是从别的OJ上搞的对不对??? CJOJ 洛谷 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味 ...

  6. 【BZOJ1924】【SDOI2010】所驼门王的宝藏(Tarjan,SPFA)

    题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为"先知"的Alpaca L. Sotomon是这个家族的领袖,外人也称其为"所驼门王". ...

  7. 【USACO4.2】草地排水Drainage Ditches(最大流)

    题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...

  8. luogu1402 酒店之王

    题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜. ...

  9. Red Hat Enterprise Linux7 配置Tomcat

    笔者是Java前端的一个萌新,电脑刚刚经历了一番脱胎换骨,然后重新装了Win10Pro,所有的开发工具都要重新安装,纠结了一番以后决定还是把一些开发工具从Windows上转移到Linux上,首先考虑了 ...

  10. python DNS域名轮询业务监控

    应用场景: 目前DNS支持一个域名对应多个IP的解析,优势是可以起到负载均衡的作用,最大的问题是目标主机不可用时无法自动剔除,因此必须在自己的业务端写好监控与发现,怎么样来做这样的监控,以python ...