Java 信号量Semaphore的实现

所属分类: 软件编程 / java 阅读数: 31
收藏 0 赞 0 分享

近日于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为阻塞状态,则获取到刚刚发布的许可,该线程被重新启用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Springmvc restful配置遇到的小坑

本文是小编给大家带了的Springmvc restful配置遇到的小小坑,小编给大家带来了问题原因及解决办法,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
收藏 0 赞 0 分享

Java中的匿名内部类小结

java内部类分为: 成员内部类、静态嵌套类、方法内部类、匿名内部类。这篇文章主要介绍了Java中的匿名内部类的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java的云打印Lodop

这篇文章主要介绍了Java的云打印Lodop 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java线程池框架核心代码解析

这篇文章主要针对Java线程池框架核心代码进行详细解析,分析Java线程池框架的实现ThreadPoolExecutor,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java 交换两个变量的数值实现方法

下面小编就为大家带来一篇Java 交换两个变量的数值实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

全面了解JAVA_BaseDAO数据处理类

下面小编就为大家带来一篇全面了解JAVA_BaseDAO数据处理类。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java、python、JavaScript以及jquery循环语句的区别

本篇文章主要介绍java、python、JavaScript以及jquery的循环语句的区别,这里整理了它们循环语句语法跟示例,以便大家阅读,更好的区分它们的不同
收藏 0 赞 0 分享

基于JDBC封装的BaseDao(实例代码)

下面小编就为大家带来一篇基于JDBC封装的BaseDao(实例代码)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

简单通用JDBC辅助类封装(实例)

下面小编就为大家带来一篇简单通用JDBC辅助类封装(实例)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈java线程中生产者与消费者的问题

下面小编就为大家带来一篇浅谈java线程中生产者与消费者的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多