Java 信号量Semaphore的实现

作者:全王工二一 时间:2023-06-19 11:00:34 

近日于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

标签:Java,信号量,Semaphore
0
投稿

猜你喜欢

  • Java中闭包简单代码示例

    2023-11-08 23:09:48
  • C#实现读取指定盘符硬盘序列号的方法

    2023-05-23 15:06:39
  • SpringBoot文件分片上传教程

    2023-07-21 21:08:40
  • 在android中实现类似uc和墨迹天气的左右拖动效果

    2022-06-18 08:47:50
  • C#反射应用实例

    2023-11-03 14:47:46
  • Java设计模式开发中使用观察者模式的实例教程

    2021-06-22 15:54:19
  • Android动画之小球拟合动画实例

    2023-06-11 00:47:46
  • spring mvc中的@ModelAttribute注解示例介绍

    2023-10-15 07:07:06
  • Android离线Doc文档访问速度慢的有效解决方法

    2021-06-27 02:10:39
  • unity实现玻璃效果

    2023-03-17 07:08:28
  • opencv实现读取视频保存视频

    2021-08-16 15:57:50
  • C# IQueryable及IEnumerable区别解析

    2021-11-27 13:46:36
  • 使用注解@Validated效验VO参数是否合规

    2023-10-27 20:13:01
  • Android后端服务器的搭建方法

    2022-05-07 19:38:04
  • Java数据结构之线段树详解

    2022-09-03 08:13:32
  • 解决在for循环中remove list报错越界的问题

    2022-01-12 15:27:56
  • RocketMQ之Consumer整体介绍启动源码分析

    2022-06-04 03:53:31
  • Android App自动更新之通知栏下载

    2023-11-07 16:56:45
  • 详解Kotlin和anko融合进行Android开发

    2021-08-15 19:34:16
  • 详解SpringCloud微服务架构之Hystrix断路器

    2022-03-05 05:28:56
  • asp之家 软件编程 m.aspxhome.com