Semaphore(一)学习

一、定义

Semaphore是一种在多线程环境下使用的设施,该设施负责协调各个线程,以保证它们能够正确、合理的使用公共资源的设施,也是操作系统中用于控制进程同步互斥的量。
Semaphore分为单值和多值两种,前者只能被一个线程获得,后者可以被若干个线程获得。
以一个停车场是运作为例。为了简单起见,假设停车场只有三个车位,一开始三个车位都是空的。这时如果同时来了五辆车,看门人允许其中三辆不受阻碍的进入,然后放下车拦,剩下的车则必须在入口等待,此后来的车也都不得不在入口处等待。这时,有一辆车离开停车场,看门人得知后,打开车拦,放入一辆,如果又离开两辆,则又可以放入两辆,如此往复。
【Semaphore(一)学习】在这个[停车场系统]中,车位是公共资源,每辆车好比一个线程,看门人起的就是[信号量]的作用。
更进一步,信号量的特性如下:信号量是一个非负整数(车位数),所有通过它的线程(车辆)都会将该整数减一(通过它当然是为了使用资源),当该整数值为零时,所有试图通过它的线程都将处于等待状态。在信号量上我们定义两种操作: Wait(等待) 和 Release(释放)。 当一个线程调用Wait(等待)操作时,它要么通过然后将信号量减一,要么一直等下去,直到信号量大于一或超时。Release(释放)实际上是在[信号量]上执行加操作,对应于车辆离开停车场,该操作之所以叫做“释放”是因为加操作实际上是释放了由信号量守护的资源。
在java中,还可以设置该信号量是否采用公平模式,如果以公平方式执行,则线程将会按到达的顺序(FIFO)执行,如果是非公平,则可以后请求的有可能排在队列的头部。 -------来源于百度百科
二、构造方法 //创建一个 Semaphore与给定数量的许可证和非公平公平设置。
public Semaphore(int permits)
//获取非公平方式(false)或者公平方式(true)
//当公平设置为真时,信号量保证调用acquire方法的线程被选择以按照它们调用这些方法的顺序获得许可(先进先出; FIFO)。
//设置为false时,此类不会保证线程获取许可的顺序
public Semaphore(int permits, boolean fair)
三、常用方法
下列只注释有参方法,无参一般默认n=1
void acquire()
void acquire(int n):从此信号量获取给定数目许可,在提供这些许可前一直将线程阻塞。比如n=2,就相当于一辆车占了两个车位。
void release()
void release(int n):释放n个许可,将其返回给信号量。就如同车开走返回n个车位。
int availablePermits():当前可用的许可数。
boolean tryAcquire(long timeout, TimeUnit unit) 尝试在指定的某个时间段内获取1个许可,获取获取不到则返回false
boolean tryAcquire(int permits, long timeout, TimeUnit unit)尝试在指定的某个时间段内获取permits个许可,获取获取不到则返回false
四、Model
1、多进路-多处理-多出路实验 本实验目标是允许多个线程同时处理任务。 1.1 SemaphoreTest.java import java.time.LocalDate; import java.util.concurrent.Semaphore; public class SemaphoreTest { private Semaphore semaphore = new Semaphore(3); void sayhello() { try { semaphore.acquire(); System.out.println("SemaphoreTest.sayhello,ThreadName:" + Thread.currentThread().getName() + "~~start"); System.out.println("begin sayHello:" + LocalDate.now()); for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " say:" + (i + 1)); } System.out.println("end sayHello:" + LocalDate.now()); semaphore.release(); System.out.println("SemaphoreTest.sayhello,ThreadName:" + Thread.currentThread().getName() + "~~end"); } catch (Exception e) { } } } 1.2 ThreadATest.java public class ThreadATestextends Thread{ private SemaphoreTest service; public ThreadATest(SemaphoreTest service) { this.service = service; } @Override public void run(){ service.sayhello(); } } 1.3 Run.java public class Run { public static void main(String[] args) { SemaphoreTest semaphoreTest = new SemaphoreTest(); ThreadATest[] threadATests = new ThreadATest[12]; for (int i = 0; i < threadATests.length; i++) { threadATests[i] = new ThreadATest(semaphoreTest); threadATests[i].start(); } } }结果: SemaphoreTest.sayhello,ThreadName:Thread-0~~start SemaphoreTest.sayhello,ThreadName:Thread-1~~start SemaphoreTest.sayhello,ThreadName:Thread-2~~start begin sayHello:2019-03-21 begin sayHello:2019-03-21 begin sayHello:2019-03-21 Thread-2 say:1 Thread-1 say:1 Thread-2 say:2 Thread-2 say:3 Thread-0 say:1 Thread-2 say:4 Thread-1 say:2 Thread-2 say:5 Thread-0 say:2 Thread-0 say:3 Thread-1 say:3 Thread-0 say:4 end sayHello:2019-03-21 Thread-0 say:5 Thread-1 say:4 Thread-1 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-2~~end SemaphoreTest.sayhello,ThreadName:Thread-4~~start begin sayHello:2019-03-21 Thread-4 say:1 Thread-4 say:2 SemaphoreTest.sayhello,ThreadName:Thread-0~~end end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-1~~end SemaphoreTest.sayhello,ThreadName:Thread-5~~start SemaphoreTest.sayhello,ThreadName:Thread-3~~start begin sayHello:2019-03-21 Thread-4 say:3 Thread-5 say:1 begin sayHello:2019-03-21 Thread-5 say:2 Thread-4 say:4 Thread-4 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-4~~end Thread-5 say:3 Thread-5 say:4 Thread-5 say:5 Thread-3 say:1 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-6~~start SemaphoreTest.sayhello,ThreadName:Thread-7~~start SemaphoreTest.sayhello,ThreadName:Thread-5~~end Thread-3 say:2 Thread-3 say:3 Thread-3 say:4 Thread-3 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-3~~end begin sayHello:2019-03-21 Thread-7 say:1 begin sayHello:2019-03-21 Thread-7 say:2 SemaphoreTest.sayhello,ThreadName:Thread-8~~start Thread-7 say:3 begin sayHello:2019-03-21 Thread-6 say:1 Thread-8 say:1 Thread-8 say:2 Thread-8 say:3 Thread-8 say:4 Thread-6 say:2 Thread-6 say:3 Thread-6 say:4 Thread-7 say:4 Thread-7 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-7~~end Thread-6 say:5 Thread-8 say:5 SemaphoreTest.sayhello,ThreadName:Thread-9~~start end sayHello:2019-03-21 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-10~~start SemaphoreTest.sayhello,ThreadName:Thread-8~~end begin sayHello:2019-03-21 Thread-10 say:1 Thread-10 say:2 Thread-10 say:3 begin sayHello:2019-03-21 Thread-10 say:4 SemaphoreTest.sayhello,ThreadName:Thread-11~~start SemaphoreTest.sayhello,ThreadName:Thread-6~~end begin sayHello:2019-03-21 Thread-11 say:1 Thread-11 say:2 Thread-10 say:5 Thread-9 say:1 end sayHello:2019-03-21 Thread-11 say:3 SemaphoreTest.sayhello,ThreadName:Thread-10~~end Thread-9 say:2 Thread-9 say:3 Thread-9 say:4 Thread-11 say:4 Thread-9 say:5 Thread-11 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-9~~end end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-11~~end 2、多进路-单处理-多出路实验 本实验目标是允许多个线程同时处理任务,但是执行的书序确是同步的,也就是阻塞的,所以也称作单处理。 2.1 修改SemaphoreTest.java import java.time.LocalDate; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.ReentrantLock; public class SemaphoreTest { private Semaphore semaphore = new Semaphore(3); private ReentrantLock lock = new ReentrantLock(); void sayhello() { try { semaphore.acquire(); System.out.println("SemaphoreTest.sayhello,ThreadName:" + Thread.currentThread().getName() + "~~start"); lock.lock(); System.out.println("begin sayHello:" + LocalDate.now()); for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " say:" + (i + 1)); } System.out.println("end sayHello:" + LocalDate.now()); lock.unlock(); semaphore.release(); System.out.println("SemaphoreTest.sayhello,ThreadName:" + Thread.currentThread().getName() + "~~end"); } catch (Exception e) { } } } 结果: SemaphoreTest.sayhello,ThreadName:Thread-0~~start SemaphoreTest.sayhello,ThreadName:Thread-2~~start SemaphoreTest.sayhello,ThreadName:Thread-1~~start begin sayHello:2019-03-21 Thread-0 say:1 Thread-0 say:2 Thread-0 say:3 Thread-0 say:4 Thread-0 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-0~~end SemaphoreTest.sayhello,ThreadName:Thread-3~~start begin sayHello:2019-03-21 Thread-1 say:1 Thread-1 say:2 Thread-1 say:3 Thread-1 say:4 Thread-1 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-1~~end begin sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-4~~start Thread-2 say:1 Thread-2 say:2 Thread-2 say:3 Thread-2 say:4 Thread-2 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-2~~end begin sayHello:2019-03-21 Thread-3 say:1 Thread-3 say:2 Thread-3 say:3 Thread-3 say:4 Thread-3 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-3~~end begin sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-6~~start SemaphoreTest.sayhello,ThreadName:Thread-5~~start Thread-4 say:1 Thread-4 say:2 Thread-4 say:3 Thread-4 say:4 Thread-4 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-4~~end begin sayHello:2019-03-21 Thread-6 say:1 Thread-6 say:2 Thread-6 say:3 Thread-6 say:4 SemaphoreTest.sayhello,ThreadName:Thread-7~~start Thread-6 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-6~~end begin sayHello:2019-03-21 Thread-5 say:1 Thread-5 say:2 Thread-5 say:3 Thread-5 say:4 SemaphoreTest.sayhello,ThreadName:Thread-8~~start Thread-5 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-5~~end begin sayHello:2019-03-21 Thread-7 say:1 SemaphoreTest.sayhello,ThreadName:Thread-9~~start Thread-7 say:2 Thread-7 say:3 Thread-7 say:4 Thread-7 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-7~~end begin sayHello:2019-03-21 Thread-8 say:1 Thread-8 say:2 Thread-8 say:3 Thread-8 say:4 Thread-8 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-8~~end SemaphoreTest.sayhello,ThreadName:Thread-10~~start begin sayHello:2019-03-21 Thread-10 say:1 Thread-10 say:2 Thread-10 say:3 Thread-10 say:4 Thread-10 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-10~~end begin sayHello:2019-03-21 Thread-9 say:1 Thread-9 say:2 Thread-9 say:3 Thread-9 say:4 Thread-9 say:5 end sayHello:2019-03-21 SemaphoreTest.sayhello,ThreadName:Thread-9~~end SemaphoreTest.sayhello,ThreadName:Thread-11~~start begin sayHello:2019-03-21 Thread-11 say:1 Thread-11 say:2 Thread-11 say:3 Thread-11 say:4 Thread-11 say:5 end sayHello:2019-03-21

五、总结
Semaphore主要用于控制当前活动线程数目,就如同停车场系统一般,而Semaphore则相当于看守的人,用于控制总共允许停车的停车位的个数,而对于每辆车来说就如同一个线程,线程需要通过acquire()方法获取许可,而release()释放许可。如果许可数达到最大活动数,那么调用acquire()之后,便进入等待队列,等待已获得许可的线程释放许可,从而使得多线程能够合理的运行。

    推荐阅读