Java 信号量Semaphore的实现
作者:全王工二一 发布时间:2023-06-19 11:00:34
标签:Java,信号量,Semaphore
近日于LeetCode看题遇1114 按序打印,获悉一解法使用了Semaphore,顺势研究,记心得于此。
此解视Semaphore为锁,以保证同一时刻单线程的顺序执行。在此原题上,我作出如下更改。
package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreDemo {
static Semaphore A;
static Semaphore B;
static Semaphore C;
public static void main(String[] args) throws InterruptedException {
A = new Semaphore(1);
B = new Semaphore(0);
C = new Semaphore(0);
ExecutorService ex=Executors.newFixedThreadPool(10);
for (int i = 0; i <7; i++) {
ex.execute(new R1());
ex.execute(new R2());
ex.execute(new R3());
}
ex.shutdown();
}
public static class R1 implements Runnable{
@Override
public void run() {
try {
// A.acquire();
System.out.println("1"+Thread.currentThread().getName());
// B.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R2 implements Runnable{
@Override
public void run() {
try {
// B.acquire();
System.out.println("2"+Thread.currentThread().getName());
// C.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R3 implements Runnable{
@Override
public void run() {
try {
// C.acquire();
System.out.println("3"+Thread.currentThread().getName());
// A.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
10个线程的常量池中,分别调用R1,R2,R3的方法多次,控制台输出对应各方法名拼接执行该方法的线程名。多次执行结果各不相同:
1pool-1-thread-1
2pool-1-thread-2
1pool-1-thread-4
3pool-1-thread-6
2pool-1-thread-5
3pool-1-thread-3
1pool-1-thread-7
2pool-1-thread-8
3pool-1-thread-9
3pool-1-thread-1
2pool-1-thread-8
1pool-1-thread-4
3pool-1-thread-1
1pool-1-thread-2
2pool-1-thread-9
1pool-1-thread-10
3pool-1-thread-1
2pool-1-thread-5
1pool-1-thread-6
3pool-1-thread-4
2pool-1-thread-8
1pool-1-thread-1
2pool-1-thread-2
3pool-1-thread-3
1pool-1-thread-4
2pool-1-thread-5
3pool-1-thread-6
1pool-1-thread-7
2pool-1-thread-8
3pool-1-thread-9
1pool-1-thread-10
3pool-1-thread-1
1pool-1-thread-4
2pool-1-thread-8
3pool-1-thread-3
2pool-1-thread-10
1pool-1-thread-2
2pool-1-thread-9
3pool-1-thread-4
1pool-1-thread-7
3pool-1-thread-6
2pool-1-thread-5
方法能调用,多线程下却无法保证方法的顺序执行。使用Semaphore后,代码为:
package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreDemo {
static Semaphore A;
static Semaphore B;
static Semaphore C;
public static void main(String[] args) throws InterruptedException {
A = new Semaphore(1);
B = new Semaphore(0);
C = new Semaphore(0);
ExecutorService ex=Executors.newFixedThreadPool(10);
for (int i = 0; i <7; i++) {
ex.execute(new R1());
ex.execute(new R2());
ex.execute(new R3());
}
ex.shutdown();
}
public static class R1 implements Runnable{
@Override
public void run() {
try {
A.acquire();
System.out.println("1"+Thread.currentThread().getName());
B.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R2 implements Runnable{
@Override
public void run() {
try {
B.acquire();
System.out.println("2"+Thread.currentThread().getName());
C.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R3 implements Runnable{
@Override
public void run() {
try {
C.acquire();
System.out.println("3"+Thread.currentThread().getName());
A.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
多次运行结果皆能保证1、2、3的顺序:
1pool-1-thread-1
2pool-1-thread-2
3pool-1-thread-3
1pool-1-thread-4
2pool-1-thread-5
3pool-1-thread-6
1pool-1-thread-7
2pool-1-thread-8
3pool-1-thread-9
1pool-1-thread-10
2pool-1-thread-1
3pool-1-thread-2
1pool-1-thread-3
2pool-1-thread-4
3pool-1-thread-5
1pool-1-thread-6
2pool-1-thread-9
3pool-1-thread-7
1pool-1-thread-10
2pool-1-thread-8
3pool-1-thread-1
附上api文档链接 Semaphore
A = new Semaphore(1);
B = new Semaphore(0);
C = new Semaphore(0);
进入R2、R3方法的线程会执行acquire()方法,而B、C中的计数器为0获取不到许可,阻塞直到一个可用,或者线程被中断,不能继续执行。R1方法中A尚有1个许可可拿到,方法执行,并给B发布一个许可,若B先于A执行acquire(),此时B为阻塞状态,则获取到刚刚发布的许可,该线程被重新启用。
来源:https://blog.csdn.net/laybarbarian/article/details/98586742
0
投稿
猜你喜欢
- 背景今天面试字节算法岗时被问到的问题,让我用C++实现一个softmax函数。softmax是逻辑回归在多分类问题上的推广。大概的公式如下:
- 前言为什么用动静态库我们在实际开发中,经常要使用别人已经实现好的功能,这是为了开发效率和鲁棒性(健壮性);因为那些功能都是顶尖的工程师已经写
- 首先打开 Visual Studio Installer 可以看到vs2022 只支持安装4.6及以上的版本,如图所示。那么该如何安装4.6
- 关于UIToolbarToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的C
- 已知字符串“aabbbcddddeeffffghijklmnopqrst”编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2)/**
- 您已经看到很多包含视频内容的应用程序,比如带有视频教程的食谱应用程序、电影应用程序和体育相关的应用程序。您是否想知道如何将视频内容添加到您的
- [LeetCode] 9. Palindrome Number 验证回文数字Determine whether an integer is
- 本Demo为练手小项目,主要是熟悉目前主流APP的架构模式.此项目中采用MVC设计模式,纯代码和少许XIB方式实现.主要实现了朋友圈功能和摇
- 前言本文的记录如何用CustomPaint、GestureDetector实现一个进度条控件。首先需要说明的是 flutter Materi
- 目录效果展示剪裁 WidgetClipRRect(圆角矩形剪裁)其他属性其他形状剪裁ClipOval(椭圆剪裁)其他属性ClipRect(矩
- 本文介绍了Flutter 通过Clipper实现各种自定义形状的示例代码,分享给大家,具体如下:ClipOval 圆形裁剪ClipOval(
- 目录简介官方示例错误示例解决方法一:Scaffold的子组件通过Builder构建解决方法二:使用GlobalKey存储ScaffoldSt
- 已知两个链表list1和list,2,各自非降序排列,将它们合并成另外一个链表list3,并且依然有序,要求保留所有节点。实现过程中,lis
- 无论哪种界面框架输入文本框都是非常重要的控件, 但是发现flutter中的输入框TextField介绍的虽然多,但是各个属性怎么组合满足需要
- 一、前言在Spring中,事务有两种实现方式:编程式事务管理: 编程式事务管理使用TransactionTemplate可实现更细
- 逆时针画圆弧,原理:将360度分割成36份,分别标出每10度角度时的坐标点,然后将每个点连接起来。 #include <io
- 前言通过此篇文章,你将了解到:Flutter windows和Android桌面应用屏幕适配的解决方案;屏幕适配的相关知识和原理;flutt
- 讲这个例子前,咱们先来看一个简单的程序:字符串数组实现数字转字母:#include <stdio.h>#include <
- 概述在移动应用开发中,消息推送可以说是一项非常重要的功能,它能够起到提醒或者唤醒用户的作用,同时也是产品运营人员更高效地实现运营目标的重要手
- 前言最近看插件库上少有的取色器大都是圆形的或者奇奇怪的的亚子,所以今天做两个矩形的颜色取色器提示:以下是本篇文章正文内容,下面案例可供参考一