时间:2021-05-20
近日于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-12pool-1-thread-21pool-1-thread-43pool-1-thread-62pool-1-thread-53pool-1-thread-31pool-1-thread-72pool-1-thread-83pool-1-thread-93pool-1-thread-12pool-1-thread-81pool-1-thread-43pool-1-thread-11pool-1-thread-22pool-1-thread-91pool-1-thread-103pool-1-thread-12pool-1-thread-51pool-1-thread-63pool-1-thread-42pool-1-thread-81pool-1-thread-12pool-1-thread-23pool-1-thread-31pool-1-thread-42pool-1-thread-53pool-1-thread-61pool-1-thread-72pool-1-thread-83pool-1-thread-91pool-1-thread-103pool-1-thread-11pool-1-thread-42pool-1-thread-83pool-1-thread-32pool-1-thread-101pool-1-thread-22pool-1-thread-93pool-1-thread-41pool-1-thread-73pool-1-thread-62pool-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-12pool-1-thread-23pool-1-thread-31pool-1-thread-42pool-1-thread-53pool-1-thread-61pool-1-thread-72pool-1-thread-83pool-1-thread-91pool-1-thread-102pool-1-thread-13pool-1-thread-21pool-1-thread-32pool-1-thread-43pool-1-thread-51pool-1-thread-62pool-1-thread-93pool-1-thread-71pool-1-thread-102pool-1-thread-83pool-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为阻塞状态,则获取到刚刚发布的许可,该线程被重新启用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一、信号量(Semaphore)信号量(Semaphore)是由内核对象维护的int变量,当信号量为0时,在信号量上等待的线程会堵塞,信号量大于0时,就解除堵塞
Semaphore是一个计数信号量,它的本质是一个共享锁。信号量维护了一个信号量许可集。线程可以通过调用acquire()来获取信号量的许可;当信号量中有可用的
java中使用Semaphore构建阻塞对象池Semaphore是java5中引入的概念,叫做计数信号量。主要用来控制同时访问某个特定资源的访问数量或者执行某个
1.信号量Semaphore先说说Semaphore,Semaphore可以控制某个资源可被同时访问的个数,通过acquire()获取一个许可,如果没有就等待,
Java通过代码模拟高并发可以以最快的方式发现我们系统中潜在的线程安全性问题,此处使用Semaphore(信号量)和CountDownLatch(闭锁)搭配Ex