JUC多线程及高并发-计时器
- CountDownLatch(计数)
- CountDownLatch 例子
- CyclicBarrier
- CyclicBarrier 例子
- Semaphore (计数信号灯)
- Semaphore 例子
CountDownLatch(计数)
翻译
一种同步辅助工具,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。
CountDownLatch是用给定的计数初始化的。由于调用countDown()方法,await方法会一直阻塞,直到当前计数达到零,然后释放所有等待的线程,并立即返回任何后续的await调用。这是一种一次性现象——无法重置计数。如果需要重置计数的版本,请考虑使用循环屏障。
倒计时锁存器是一种通用的同步工具,可用于多种用途。使用计数1初始化的CountDownLatch用作一个简单的开/关锁存器或门:所有调用countDown()的线程都在门处等待,直到它被调用countDown()的线程打开。初始化为N的倒计时锁存器可用于使一个线程等待N个线程完成某个操作,或者某个操作已完成N次。
CountDownLatch的一个有用特性是,它不要求调用countDown的线程在继续之前等待计数达到零,它只是防止任何线程通过等待,直到所有线程都可以通过。
位置
CountDownLatch 例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65package com.xin; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import java.util.concurrent.CountDownLatch; /** * @author :小心仔 * @date :Created in 2021/11/5 19:35 * @description:计数器Demo */ public class CountDownLatchDemo { public static void main(String[] args) throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(5); for (int i = 1; i <= 5; i++) { new Thread(() -> { System.out.println(Thread.currentThread().getName() + " 灭亡"); countDownLatch.countDown();// 减 1 }, CountryType.getCountryType(i)).start(); } // 停止 countDownLatch.await(); System.out.println(Thread.currentThread().getName() + " 国灭亡"); } } enum CountryType { ONE(1, "秦国"), TWO(2, "楚国"), THREE(3, "赵国"), FOUR(4, "燕国"), FIVE(5, "柳国"); private Integer code; private String name; public static String getCountryType(int code) { CountryType[] values = CountryType.values(); for (CountryType val : values ) { if(val.code == code){ return val.name; } } return null; } CountryType(int code, String name) { this.code = code; this.name = name; } public Integer getCode() { return code; } public String getName() { return name; } }
CyclicBarrier
翻译
一种同步辅助工具,允许一组线程都等待对方到达一个共同的障碍点。CyclicBarrier在涉及固定大小的线程组的程序中很有用,这些线程有时必须互相等待。该屏障被称为循环屏障,因为它可以在释放等待的线程后重新使用。
CyclicBarrier支持可选的Runnable命令,该命令在参与方中的最后一个线程到达后,但在释放任何线程之前,在每个屏障点运行一次。此屏障操作有助于在任何一方继续之前更新共享状态。
位置
CyclicBarrier 例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package com.xin; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * @author :小心仔 * @date :Created in 2021/11/5 20:00 * @description: */ public class CyclicBarrierDemo { public static void main(String[] args) { CyclicBarrier cyclicBarrier = new CyclicBarrier(7,() -> { System.out.println("召唤神龙!"); }); for (int i = 1; i <= 7; i++) { int ii = i; new Thread(() -> { System.out.println("召唤" + ii + " 个龙珠"); try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } },String.valueOf(i)).start(); } } }
Semaphore (计数信号灯)
翻译
计数信号灯。从概念上讲,信号量维护一组许可。如果需要,每个acquire()都会阻塞,直到有许可证可用,然后再获取它。每次释放()都会添加一个许可证,可能会释放阻止收单机构。但是,没有使用实际的许可证对象;信号量只保留可用数量的计数,并相应地进行操作。
信号量通常用于限制可以访问某些(物理或逻辑)资源的线程数。例如,下面是一个使用信号量控制对项目池访问的类
位置
Semaphore 例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package com.xin; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; /** * @author :小心仔 * @date :Created in 2021/11/5 20:11 * @description: */ public class SemaphoreDemo { public static void main(String[] args) { Semaphore semaphore = new Semaphore(3); // 三个车位 for (int i = 1; i <= 7; i++) { // 模拟7辆汽车 new Thread(() -> { try { semaphore.acquire(); System.out.println(Thread.currentThread().getName() + " t 抢到车位"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace();} System.out.println(Thread.currentThread().getName() + " t 停车 3秒让出车位"); } catch (InterruptedException e) { e.printStackTrace(); }finally { semaphore.release(); } },String.valueOf(i)).start(); } } }
最后
以上就是秀丽墨镜最近收集整理的关于JUC多线程及高并发-CountDownLatch、CyclicBarrier、Semaphore的全部内容,更多相关JUC多线程及高并发-CountDownLatch、CyclicBarrier、Semaphore内容请搜索靠谱客的其他文章。
发表评论 取消回复