当前位置:   article > 正文

Java多线程(六)——多线程的阻塞队列_队列 多现场租塞获取

队列 多现场租塞获取

 

 

目录

一、引言

二、阻塞队列种类

三、阻塞队列使用

四、阻塞队列实现原理

五、总结

 


 

一、引言

 

阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空。当队列满时,存储元素的线程会等待队列可用。

阻塞队列常用于生产者和消费者的场景,生产者线程可以把生产结果存到阻塞队列中,而消费者线程把中间结果取出并在将来修改它们。比如自助火锅传送带送菜,每当我们拿下一盘菜的时候,服务人员才能添加到上面,当传送带上没有东西的时候我们必须等着服务员放菜。

 

 

二、阻塞队列种类

 

  • ArrayBlockingQueue :一个由数组结构组成的有界阻塞队列。

  • LinkedBlockingQueue :一个由链表结构组成的有界阻塞队列。

  • PriorityBlockingQueue :一个支持优先级排序的无界阻塞队列。

  • DelayQueue:一个使用优先级队列实现的无界阻塞队列。

  • SynchronousQueue:一个不存储元素的阻塞队列。

  • LinkedTransferQueue:一个由链表结构组成的无界阻塞队列。

  • LinkedBlockingDeque:一个由链表结构组成的双向阻塞队列。

 

 

三、阻塞队列使用

 

每0.4秒中生产一辆车,每1秒中提取一辆车,所以车厂总是会有车

  1. public class TestQueue {
  2. public static void main(String []args)
  3. {
  4. final Object o=new Object();
  5. final BlockingQueue<String> queue=new ArrayBlockingQueue<String>(2);
  6. for(int i=0;i<2;i++)
  7. {
  8. new Thread(new Runnable() {
  9. public void run() {
  10. while (true) {
  11. try {
  12. Thread.sleep(400);
  13. System.out.println(Thread.currentThread().getName() + "准备生产汽车");
  14. queue.put("qiche");
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. System.out.println(Thread.currentThread().getName() + "生产汽车完毕,已有" + queue.size() + "辆汽车");
  19. }
  20. }
  21. }).start();
  22. }
  23. new Thread(new Runnable() {
  24. public void run() {
  25. while (true) {
  26. try {
  27. //Thread.sleep((int)Math.random()*10000000);
  28. Thread.sleep(1000);
  29. System.out.println(Thread.currentThread().getName() + "准备提走汽车");
  30. queue.take();
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. System.out.println(Thread.currentThread().getName() + "已经提走汽车,还有" + queue.size() + "辆汽车");
  35. }
  36. }
  37. }).start();
  38. }
  39. }

 

 

四、阻塞队列实现原理

 

阻塞队列实现方法使用的lock+condition高级方法来实现的,可以参考Java多线程(三)——多线程实现同步来了解lock与condition使用。

  1. public class TestQueueyuanli {
  2. public static void main(String []args)
  3. {
  4. final Object o=new Object();
  5. //final BlockingQueue<String> queue=new ArrayBlockingQueue<String>(2);
  6. final BoundedBuffer queue =new BoundedBuffer();
  7. for(int i=0;i<2;i++)
  8. {
  9. new Thread(new Runnable() {
  10. public void run() {
  11. while (true) {
  12. try {
  13. //Thread.sleep((int)Math.random()*10000000);
  14. Thread.sleep(400);
  15. System.out.println(Thread.currentThread().getName() + "准备生产汽车");
  16. queue.put("qiche");
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. System.out.println(Thread.currentThread().getName() + "生产汽车完毕,已有" + queue.size() + "辆汽车");
  21. }
  22. }
  23. }).start();
  24. }
  25. new Thread(new Runnable() {
  26. public void run() {
  27. while (true) {
  28. try {
  29. //Thread.sleep((int)Math.random()*10000000);
  30. Thread.sleep(1000);
  31. System.out.println(Thread.currentThread().getName() + "准备提走汽车");
  32. queue.take();
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36. System.out.println(Thread.currentThread().getName() + "已经提走汽车,还有" + queue.size() + "辆汽车");
  37. }
  38. }
  39. }).start();
  40. }
  41. }
  42. class BoundedBuffer {
  43. final Lock lock = new ReentrantLock();
  44. final Condition notFull = lock.newCondition();//notFull代表取能否放数据
  45. final Condition notEmpty = lock.newCondition();//notEmpoty代表能否取数据
  46. final Object[] items = new Object[5];
  47. int putptr, takeptr, count;
  48. public void put(Object x) throws InterruptedException {
  49. lock.lock(); try {
  50. while (count == items.length)
  51. notFull.await();
  52. items[putptr] = x;
  53. if (++putptr == items.length) putptr = 0;
  54. ++count;
  55. notEmpty.signal();
  56. } finally { lock.unlock(); }
  57. }
  58. public Object take() throws InterruptedException {
  59. lock.lock(); try {
  60. while (count == 0)
  61. notEmpty.await();
  62. Object x = items[takeptr];
  63. if (++takeptr == items.length) takeptr = 0;
  64. --count;
  65. notFull.signal();
  66. return x;
  67. } finally { lock.unlock(); }
  68. }
  69. public int size()
  70. {
  71. return count;
  72. }
  73. }

 

 

五、总结

 

  • 阻塞队列种类;

 

  • 阻塞队列使用;

 

  • 阻塞队列原理;

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/968488
推荐阅读
相关标签
  

闽ICP备14008679号