当前位置:   article > 正文

并发编程:并发集合:非阻塞线程安全的双端队列ConcurrentLinkedDeque_线程安全双端队列

线程安全双端队列

目录

ConcurrentLinkedDeque

一、主程序

二、向队列添加元素的任务类

三、消费队列元素的任务类

四、执行结果


ConcurrentLinkedDeque

  • add:向队列尾部添加元素
  • pollFirst/pollLast:获取并删除队首/队尾元素
  • getFirst/getLast:获取(不删除)队首/队尾元素,如果无元素,则抛出异常NoSuchElementException
  • peekFirst/peekLast:获取(不删除)队首/队尾元素,如果无元素,则返回Null
  • remove/removeFirst/removeLast:删除队首/队首/队尾元素,如果无元素,则抛出异常NoSuchElementException

一、主程序

  1. package xyz.jangle.thread.test.n7_2.concurrentlinkeddeque;
  2. import java.util.concurrent.ConcurrentLinkedDeque;
  3. /**
  4. * 7.2、非阻塞线程安全的双端队列
  5. *
  6. * @author jangle
  7. * @email jangle@jangle.xyz
  8. * @time 2020年9月10日 下午7:21:39
  9. *
  10. */
  11. public class M {
  12. public static void main(String[] args) {
  13. var list = new ConcurrentLinkedDeque<String>();
  14. var threads = new Thread[100];
  15. for (int i = 0; i < threads.length; i++) {
  16. var task = new AddTask(list);
  17. threads[i] = new Thread(task);
  18. threads[i].start();
  19. }
  20. System.out.println("M:" + threads.length + "个线程往队列中添加元素");
  21. for (int i = 0; i < threads.length; i++) {
  22. try {
  23. threads[i].join();
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. // 100 * 10000 个元素
  29. System.out.println("元素添加完毕,队列长度:" + list.size());
  30. System.out.println("开始创建消费线程");
  31. for (int i = 0; i < threads.length; i++) {
  32. var task = new PollTask(list);
  33. threads[i] = new Thread(task);
  34. threads[i].start();
  35. }
  36. System.out.println("M:" + threads.length + "个消费线程向队列获取元素");
  37. for (int i = 0; i < threads.length; i++) {
  38. try {
  39. threads[i].join();
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. // 消费 100 * 2 * 5000 剩余0个元素
  45. System.out.println("M:消费完后,队列长度:" + list.size());
  46. }
  47. }

二、向队列添加元素的任务类

  1. package xyz.jangle.thread.test.n7_2.concurrentlinkeddeque;
  2. import java.util.concurrent.ConcurrentLinkedDeque;
  3. /**
  4. * 添加元素的任务
  5. * @author jangle
  6. * @email jangle@jangle.xyz
  7. * @time 2020年9月10日 下午7:22:43
  8. *
  9. */
  10. public class AddTask implements Runnable {
  11. private final ConcurrentLinkedDeque<String> list;
  12. public AddTask(ConcurrentLinkedDeque<String> list) {
  13. super();
  14. this.list = list;
  15. }
  16. @Override
  17. public void run() {
  18. String name = Thread.currentThread().getName();
  19. for (int i = 0; i < 10000; i++) {
  20. list.add(name +":Element "+i);
  21. }
  22. }
  23. }

三、消费队列元素的任务类

  1. package xyz.jangle.thread.test.n7_2.concurrentlinkeddeque;
  2. import java.util.concurrent.ConcurrentLinkedDeque;
  3. /**
  4. * 消费队列元素的任务类
  5. * @author jangle
  6. * @email jangle@jangle.xyz
  7. * @time 2020年9月10日 下午7:24:53
  8. *
  9. */
  10. public class PollTask implements Runnable {
  11. private final ConcurrentLinkedDeque<String> list;
  12. public PollTask(ConcurrentLinkedDeque<String> list) {
  13. super();
  14. this.list = list;
  15. }
  16. @Override
  17. public void run() {
  18. for (int i = 0; i < 5000; i++) {
  19. list.pollFirst();
  20. list.pollLast();
  21. }
  22. }
  23. }

四、执行结果

  1. M:100个线程往队列中添加元素
  2. 元素添加完毕,队列长度:1000000
  3. 开始创建消费线程
  4. M:100个消费线程向队列获取元素
  5. M:消费完后,队列长度:0

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

闽ICP备14008679号