title: 并发-JUC工具包top: false
cover: true
author: 张文军
date: 2020-07-19 18:38:40
tags:
- 并发-JUC工具包
- 并发
- Atomic
- CylicBarrier
- CountDownLatch
- Semaphore
category: 并发
summary: 并发- Atomic- CylicBarrier- CountDownLatch- Semaphore

Java快速开发学习

锁清秋

  1. package fuxi.concurrency;
  2. import org.junit.Test;
  3. import java.util.concurrent.*;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. import static java.lang.System.out;
  6. import static java.lang.Thread.currentThread;
  7. import static java.util.concurrent.TimeUnit.SECONDS;
  8. /**
  9. * @Description: TODO
  10. * @author 张文军
  11. * @Company: njauit.cn
  12. * @version: 1.0
  13. * @date 2020/7/1816:17
  14. */
  15. public class JUC {
  16. private Executor executor = ThreadPool.getThreadPool();
  17. /**
  18. * Atomic 原子操作
  19. *
  20. * 分类:
  21. *
  22. * 基本类型:
  23. * 整形(AtomicInteger)、长整型(AtomicLong)、布尔型(AtomicBoolean)
  24. *
  25. * 数组类型:使用原子的方式更新数组里的某个元素
  26. * 整形(AtomicIntegerArray)、长整型(AtomicLongArray)、引用类型(AtomicReferenceArray)
  27. *
  28. * 引用类型
  29. * AtomicReference:引用类型原子类、AtomicStampedReference:原子更新引用类型里的字段原子类、
  30. * AtomicMarkableReference :原子更新带有标记位的引用类型
  31. */
  32. @Test
  33. public void atomic() {
  34. AtomicInteger atomicInteger = new AtomicInteger();
  35. out.println("atomicInteger = " + atomicInteger.incrementAndGet());
  36. }
  37. /**
  38. * Semaphore :信号量:共享资源的互斥使用,控制并发数
  39. */
  40. @Test
  41. public void semaphoreTest() {
  42. /**
  43. * 一个厕所有三个坑,每次只能最多同时有三个人使用
  44. */
  45. Semaphore toilet = new Semaphore(3);
  46. /**
  47. * 上厕所的人数
  48. */
  49. int beauty = 20;
  50. while (beauty-- > 0) {
  51. new Thread(() -> {
  52. //抢坑
  53. try {
  54. toilet.acquire();
  55. out.println(currentThread().getName() + "抢到了坑");
  56. out.println(currentThread().getName() + "正在上厕所。。。。。。。。。。");
  57. try { SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. } finally {
  61. out.println(currentThread().getName() + "厕所上完了");
  62. toilet.release();
  63. }
  64. }, "美女--" + beauty + " 号").start();
  65. }
  66. while (true)
  67. ;
  68. }
  69. /**
  70. * CountDownLatch : 倒数计数器:让一个线程阻塞等待其他线程执行完成后才执行
  71. */
  72. @Test
  73. public void countDownLatch() {
  74. /**
  75. * 上完晚自习后班长锁门:只有当学生都走完了他才可以锁门
  76. */
  77. CountDownLatch student = new CountDownLatch(10);
  78. int studentsNumber = 10;
  79. while (studentsNumber-- > 0) {
  80. new Thread(() -> {
  81. // TODO:
  82. out.println(currentThread().getName() + " 关门离开了!");
  83. student.countDown();
  84. }, studentsNumber + "号学生").start();
  85. }
  86. new Thread(() -> {
  87. // TODO:
  88. try {
  89. /**
  90. * 如果计数器不为0,班长就一直阻塞等待;
  91. */
  92. student.await();
  93. out.println(currentThread().getName() + "锁门离开!");
  94. } catch (InterruptedException e) {
  95. e.printStackTrace();
  96. }
  97. }, "monitor").start();
  98. while (true)
  99. ;
  100. }
  101. /**
  102. * CyclicBarrier : 循环栅栏:
  103. */
  104. @Test
  105. public void cyclicBarrier() {
  106. /**
  107. * 银行保险柜有五把钥匙,分别由5位经理保管,只有五把钥匙同时插入的时候才能打开,
  108. * 但五位经理来上班的时间各不相同,所以要等经理们都到了才可以开启保险柜;
  109. */
  110. CyclicBarrier bankKey = new CyclicBarrier(5);
  111. int numberOfManagers = 5;
  112. while (numberOfManagers-- > 0) {
  113. new Thread(() -> {
  114. out.println(currentThread().getName() + "正在赶来的路上。。。");
  115. try { TimeUnit.SECONDS.sleep(((int) (Math.random() * 10)) % 3); } catch (InterruptedException e) {
  116. e.printStackTrace();
  117. }
  118. out.println(currentThread().getName() + "到了!");
  119. try {
  120. bankKey.await();
  121. out.println(currentThread().getName() + "开完保险柜后去干其他事了!");
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. }, numberOfManagers + "号经理").start();
  126. }
  127. while (true) {
  128. }
  129. }
  130. }