演示一个阻塞队列的使用

  1. public class BlockingQueueTest
  2. {
  3. public static void main(String[] args)
  4. {
  5. //创建一个包含三个元素的阻塞队列
  6. final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
  7. for (int i = 0; i < 2; i++)
  8. {
  9. new Thread(new Runnable()
  10. {
  11. @Override
  12. public void run()
  13. {
  14. while(true){
  15. try
  16. {
  17. Thread.sleep(1000);
  18. System.out.println("线程"+Thread.currentThread().getName()+" 准备放数据了");
  19. queue.put(new Random().nextInt(100));
  20. System.out.println("线程"+Thread.currentThread().getName()+" 已经放完数据了,目前队列有"+queue.size()+"个数据");
  21. } catch (InterruptedException e)
  22. {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }).start();
  28.  
  29. }
  30.  
  31. new Thread(new Runnable()
  32. {
  33. @Override
  34. public void run()
  35. {
  36. while(true){
  37. try
  38. {
  39. //将此处的睡眠时间改为200和2000 分别观察结果
  40. Thread.sleep(2000);
  41. System.out.println("线程"+Thread.currentThread().getName()+" 准备取数据了");
  42. queue.take();
  43. System.out.println("线程"+Thread.currentThread().getName()+" 已经取完数据了,目前队列有"+queue.size()+"个数据");
  44. } catch (InterruptedException e)
  45. {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. }).start();
  51. }
  52. }

部分运行结果如下,

  1. 线程Thread-1 准备放数据了
  2. 线程Thread-0 准备放数据了
  3. 线程Thread-1 已经放完数据了,目前队列有1个数据
  4. 线程Thread-0 已经放完数据了,目前队列有2个数据
  5. 线程Thread-2 准备取数据了
  6. 线程Thread-2 已经取完数据了,目前队列有1个数据
  7. 线程Thread-1 准备放数据了
  8. 线程Thread-0 准备放数据了
  9. 线程Thread-1 已经放完数据了,目前队列有2个数据
  10. 线程Thread-0 已经放完数据了,目前队列有3个数据
  11. 线程Thread-1 准备放数据了
  12. 线程Thread-0 准备放数据了
  13. 线程Thread-2 准备取数据了
  14. 线程Thread-2 已经取完数据了,目前队列有2个数据
  15. 线程Thread-1 已经放完数据了,目前队列有3个数据
  16. 线程Thread-1 准备放数据了
  17. 线程Thread-2 准备取数据了
  18. 线程Thread-2 已经取完数据了,目前队列有2个数据
  19. 线程Thread-0 已经放完数据了,目前队列有3个数据
  20. 线程Thread-0 准备放数据了
  21. 线程Thread-2 准备取数据了
  22. 线程Thread-2 已经取完数据了,目前队列有2个数据
  23. 线程Thread-1 已经放完数据了,目前队列有3个数据
  24. 线程Thread-1 准备放数据了
  25. 线程Thread-2 准备取数据了
  26. 线程Thread-2 已经取完数据了,目前队列有2个数据
  27. 线程Thread-0 已经放完数据了,目前队列有3个数据
  28. 线程Thread-0 准备放数据了

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

  1. public class BlockingQueueCommunication
  2. {
  3. public static void main(String[] args) throws InterruptedException
  4. {
  5. final Core core = new Core();
  6. //子线程
  7. new Thread(
  8. new Runnable()
  9. {
  10. @Override
  11. public void run()
  12. {
  13. for (int i = 1; i <= 50;i++)
  14. {
  15. core.SubMethod(i);
  16. }
  17. }
  18. }
  19. ).start();
  20.  
  21. //主线程
  22. for (int i = 1; i <= 50; i++)
  23. {
  24. core.MainMethod(i);
  25. }
  26. }
  27.  
  28. /**
  29. * 创建一个静态的类
  30. * 将核心的业务逻辑的方法放在这里
  31. * 体现了高内聚的特点
  32. * @author huang.jf
  33. *
  34. */
  35. static class Core{
  36. //创建两个阻塞队列 都只能装一个元素
  37. //实现的效果就是q1存,q2取,q2存,q1取
  38. //用两个具有一个空间的队列来实现同步通知的功能
  39. private BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
  40. private BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
  41.  
  42. //匿名构造方法 也叫非静态代码块
  43. //当创建对象时 该代码块先被执行
  44. //保证初始化,在queue2队列中包含一个元素
  45. {
  46. try
  47. {
  48. queue2.put(1);
  49. } catch (InterruptedException e)
  50. {
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. //子线程调用 循环输出10次
  56. public void SubMethod(int j){
  57. try
  58. {
  59. queue1.put(1);//向q1中存入一个值
  60. } catch (InterruptedException e)
  61. {
  62. e.printStackTrace();
  63. }
  64. for (int i = 1; i <= 10; i++)
  65. {
  66. System.out.println("this is sub thread..."+i+"......."+j);
  67. }
  68. try
  69. {
  70. queue2.take();//取出q2中的一个值
  71. } catch (InterruptedException e)
  72. {
  73. e.printStackTrace();
  74. }
  75. }
  76. //主线程调用循环输出一百次
  77. public void MainMethod(int j){
  78. try
  79. {
  80. queue2.put(1);//一开始queue2队列会被阻塞,因为初始化的时候已经有一个值
  81. } catch (InterruptedException e)
  82. {
  83. e.printStackTrace();
  84. }
  85. for (int i = 1; i <= 100; i++)
  86. {
  87. System.out.println("this is main thread..."+i+"......"+j);
  88. }
  89. try
  90. {
  91. queue1.take();
  92. } catch (InterruptedException e)
  93. {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. }

部分运行结果如下:

  1. this is sub thread...1.......49
  2. this is sub thread...2.......49
  3. this is sub thread...3.......49
  4. this is sub thread...4.......49
  5. this is sub thread...5.......49
  6. this is sub thread...6.......49
  7. this is sub thread...7.......49
  8. this is sub thread...8.......49
  9. this is sub thread...9.......49
  10. this is sub thread...10.......49
  11. this is main thread...1......49
  12. this is main thread...2......49
  13. this is main thread...3......49
  14. this is main thread...4......49
  15. this is main thread...5......49
  16. this is main thread...6......49
  17. this is main thread...7......49
  18. this is main thread...8......49
  19. this is main thread...9......49
  20. this is main thread...10......49
  21. this is main thread...11......49
  22. this is main thread...12......49
  23. this is main thread...13......49
  24. this is main thread...14......49
  25. this is main thread...15......49
  26. this is main thread...16......49
  27. this is main thread...17......49
  28. this is main thread...18......49
  29. this is main thread...19......49
  30. this is main thread...20......49
  31. this is main thread...21......49
  32. this is main thread...22......49
  33. this is main thread...23......49
  34. this is main thread...24......49
  35. this is main thread...25......49
  36. this is main thread...26......49
  37. this is main thread...27......49
  38. this is main thread...28......49
  39. this is main thread...29......49
  40. this is main thread...30......49
  41. this is main thread...31......49
  42. this is main thread...32......49
  43. this is main thread...33......49
  44. this is main thread...34......49
  45. this is main thread...35......49
  46. this is main thread...36......49
  47. this is main thread...37......49
  48. this is main thread...38......49
  49. this is main thread...39......49
  50. this is main thread...40......49
  51. this is main thread...41......49
  52. this is main thread...42......49
  53. this is main thread...43......49
  54. this is main thread...44......49
  55. this is main thread...45......49
  56. this is main thread...46......49
  57. this is main thread...47......49
  58. this is main thread...48......49
  59. this is main thread...49......49
  60. this is main thread...50......49
  61. this is main thread...51......49
  62. this is main thread...52......49
  63. this is main thread...53......49
  64. this is main thread...54......49
  65. this is main thread...55......49
  66. this is main thread...56......49
  67. this is main thread...57......49
  68. this is main thread...58......49
  69. this is main thread...59......49
  70. this is main thread...60......49
  71. this is main thread...61......49
  72. this is main thread...62......49
  73. this is main thread...63......49
  74. this is main thread...64......49
  75. this is main thread...65......49
  76. this is main thread...66......49
  77. this is main thread...67......49
  78. this is main thread...68......49
  79. this is main thread...69......49
  80. this is main thread...70......49
  81. this is main thread...71......49
  82. this is main thread...72......49
  83. this is main thread...73......49
  84. this is main thread...74......49
  85. this is main thread...75......49
  86. this is main thread...76......49
  87. this is main thread...77......49
  88. this is main thread...78......49
  89. this is main thread...79......49
  90. this is main thread...80......49
  91. this is main thread...81......49
  92. this is main thread...82......49
  93. this is main thread...83......49
  94. this is main thread...84......49
  95. this is main thread...85......49
  96. this is main thread...86......49
  97. this is main thread...87......49
  98. this is main thread...88......49
  99. this is main thread...89......49
  100. this is main thread...90......49
  101. this is main thread...91......49
  102. this is main thread...92......49
  103. this is main thread...93......49
  104. this is main thread...94......49
  105. this is main thread...95......49
  106. this is main thread...96......49
  107. this is main thread...97......49
  108. this is main thread...98......49
  109. this is main thread...99......49
  110. this is main thread...100......49
  111. this is sub thread...1.......50
  112. this is sub thread...2.......50
  113. this is sub thread...3.......50
  114. this is sub thread...4.......50
  115. this is sub thread...5.......50
  116. this is sub thread...6.......50
  117. this is sub thread...7.......50
  118. this is sub thread...8.......50
  119. this is sub thread...9.......50
  120. this is sub thread...10.......50
  121. this is main thread...1......50
  122. this is main thread...2......50
  123. this is main thread...3......50
  124. this is main thread...4......50
  125. this is main thread...5......50
  126. this is main thread...6......50
  127. this is main thread...7......50
  128. this is main thread...8......50
  129. this is main thread...9......50
  130. this is main thread...10......50
  131. this is main thread...11......50
  132. this is main thread...12......50
  133. this is main thread...13......50
  134. this is main thread...14......50
  135. this is main thread...15......50
  136. this is main thread...16......50
  137. this is main thread...17......50
  138. this is main thread...18......50
  139. this is main thread...19......50
  140. this is main thread...20......50
  141. this is main thread...21......50
  142. this is main thread...22......50
  143. this is main thread...23......50
  144. this is main thread...24......50
  145. this is main thread...25......50
  146. this is main thread...26......50
  147. this is main thread...27......50
  148. this is main thread...28......50
  149. this is main thread...29......50
  150. this is main thread...30......50
  151. this is main thread...31......50
  152. this is main thread...32......50
  153. this is main thread...33......50
  154. this is main thread...34......50
  155. this is main thread...35......50
  156. this is main thread...36......50
  157. this is main thread...37......50
  158. this is main thread...38......50
  159. this is main thread...39......50
  160. this is main thread...40......50
  161. this is main thread...41......50
  162. this is main thread...42......50
  163. this is main thread...43......50
  164. this is main thread...44......50
  165. this is main thread...45......50
  166. this is main thread...46......50
  167. this is main thread...47......50
  168. this is main thread...48......50
  169. this is main thread...49......50
  170. this is main thread...50......50
  171. this is main thread...51......50
  172. this is main thread...52......50
  173. this is main thread...53......50
  174. this is main thread...54......50
  175. this is main thread...55......50
  176. this is main thread...56......50
  177. this is main thread...57......50
  178. this is main thread...58......50
  179. this is main thread...59......50
  180. this is main thread...60......50
  181. this is main thread...61......50
  182. this is main thread...62......50
  183. this is main thread...63......50
  184. this is main thread...64......50
  185. this is main thread...65......50
  186. this is main thread...66......50
  187. this is main thread...67......50
  188. this is main thread...68......50
  189. this is main thread...69......50
  190. this is main thread...70......50
  191. this is main thread...71......50
  192. this is main thread...72......50
  193. this is main thread...73......50
  194. this is main thread...74......50
  195. this is main thread...75......50
  196. this is main thread...76......50
  197. this is main thread...77......50
  198. this is main thread...78......50
  199. this is main thread...79......50
  200. this is main thread...80......50
  201. this is main thread...81......50
  202. this is main thread...82......50
  203. this is main thread...83......50
  204. this is main thread...84......50
  205. this is main thread...85......50
  206. this is main thread...86......50
  207. this is main thread...87......50
  208. this is main thread...88......50
  209. this is main thread...89......50
  210. this is main thread...90......50
  211. this is main thread...91......50
  212. this is main thread...92......50
  213. this is main thread...93......50
  214. this is main thread...94......50
  215. this is main thread...95......50
  216. this is main thread...96......50
  217. this is main thread...97......50
  218. this is main thread...98......50
  219. this is main thread...99......50
  220. 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. Linux显示内存状态

    Linux显示内存状态 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free total used free shared buffers cached M ...

  2. 1118: 属于 static 类型 Object 的值的隐式强制指令的目标可能是非相关类型 Number。

    1.错误描述 此行的多个标记: -1118: 属于 static 类型 Object 的值的隐式强制指令的目标可能是非相关类型 Number. -left 2.错误原因 /** * 刷新按钮函数 */ ...

  3. Django学习-20-信号

    Django信号                  使得某个操作之前能定制化一些任务         - 内置信号 pre_init # django的model执行其构造方法前,自动触发 post_ ...

  4. Django学习-5-模板渲染

    1. {{ 变量名 }}                          def func(request):                     return render(request, ...

  5. java SpringWeb 接收安卓android传来的图片集合及其他信息入库存储

    公司是做APP的,进公司一年了还是第一次做安卓的接口 安卓是使用OkGo.post("").addFileParams("key",File); 通过这种方式传 ...

  6. 利用Python爬虫爬取淘宝商品做数据挖掘分析实战篇,超详细教程

    项目内容 本案例选择>> 商品类目:沙发: 数量:共100页  4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 项目目的 1. 对商品标题进行文本分析 词云可视化 2. ...

  7. luoguP2711 小行星

    题目描述 星云中有n颗行星,每颗行星的位置是(x,y,z).每次可以消除一个面(即x,y或z坐标相等)的行星,但是由于时间有限,求消除这些行星的最少次数. 输入输出格式 输入格式: 第1行为小行星个数 ...

  8. [BZOJ1880] [Sdoi2009] Elaxia的路线 (SPFA & 拓扑排序)

    Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w**每天都要奔波于宿舍和实验室之间, ...

  9. 【Android】[Problem]-"Waiting for target device to come online".

    环境: win10专业版(创意者),Android studio 2.3.1 问题描述: 安装玩Android studio之后创建一个项目,建立AVD之后,运行程序时一直不能启动AVD,具体描述为: ...

  10. mac php 版本切换

    注意:要求所有php版本都是由brew安装 一.使用brew安装php多版本方法 # brew install php56 # brew install php70 二.安装切换工具 # brew i ...