Semaphore概述
- 信号量,可以控制并发访问的线程个数;
- 可以很容易的控制某个资源被同时访问的个数;
- 就很像三峡大坝对流量的控制;
Semaphore使用场景
- 用于仅能提供优先访问的资源,比如数据库的连接,最大只有20;
Semaphore示例(一) 1次拿1个许可
- 对要做并发控制的代码,前有要包裹上Semaphore相关的函数;
- 控制同时只能有3个线程同时执行,每个线程的执行时间是1s;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@Slf4j
public class SemaphoreExample1 {
private final static int threadCount = 10;
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(3);
for (int i = 0; i < threadCount; i++) {
final int threadNum = i;
exec.execute(() -> {
try {
semaphore.acquire(); // 获取一个许可
test(threadNum);
semaphore.release(); // 释放一个许可
} catch (Exception e) {
log.error("exception", e);
}
});
}
exec.shutdown();
}
private static void test(int threadNum) throws Exception {
log.info("{}", threadNum);
Thread.sleep(1000);
}
}
输出:
- 每秒输出3行;
18:18:59.749 [pool-1-thread-2] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 1
18:18:59.749 [pool-1-thread-3] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 2
18:18:59.749 [pool-1-thread-1] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 0
18:19:00.757 [pool-1-thread-4] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 3
18:19:00.757 [pool-1-thread-5] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 4
18:19:00.757 [pool-1-thread-6] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 5
18:19:01.757 [pool-1-thread-7] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 6
18:19:01.757 [pool-1-thread-10] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 9
18:19:01.757 [pool-1-thread-8] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 7
18:19:02.757 [pool-1-thread-9] INFO com.example.concurrency.example.aqs.SemaphoreExample1 - 8
Semaphore示例(二) 1次拿n个许可
- Semaphore控制同时执行的线程数为3;
- 线程每次获取3个许可,即每个线程都独占闸口,然后释放3个许可;
- 输出的时候就是1次1行;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@Slf4j
public class SemaphoreExample2 {
private final static int threadCount = 5;
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(3);
for (int i = 0; i < threadCount; i++) {
final int threadNum = i;
exec.execute(() -> {
try {
semaphore.acquire(3); // 获取多个许可
test(threadNum);
semaphore.release(3); // 释放多个许可
} catch (Exception e) {
log.error("exception", e);
}
});
}
exec.shutdown();
}
private static void test(int threadNum) throws Exception {
log.info("{}", threadNum);
Thread.sleep(1000);
}
}
输出:
- 每次输出一行,1s输出1次;
18:22:33.410 [pool-1-thread-1] INFO com.example.concurrency.example.aqs.SemaphoreExample2 - 0
18:22:34.417 [pool-1-thread-2] INFO com.example.concurrency.example.aqs.SemaphoreExample2 - 1
18:22:35.417 [pool-1-thread-3] INFO com.example.concurrency.example.aqs.SemaphoreExample2 - 2
18:22:36.418 [pool-1-thread-4] INFO com.example.concurrency.example.aqs.SemaphoreExample2 - 3
18:22:37.418 [pool-1-thread-5] INFO com.example.concurrency.example.aqs.SemaphoreExample2 - 4
Semaphore示例(三) 尝试获取许可
- 如果并发量特别大,闸口就显得很很小,请求的队伍就会排的很长,大量请求迟迟得不到响应;
- 有一种场景下,允许这样的策略:过去办业务,发现有窗口就去办,需要排队就走人不办了,下面的示例就模拟了这种情况:
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@Slf4j
public class SemaphoreExample3 {
private final static int threadCount = 20;
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(3);
for (int i = 0; i < threadCount; i++) {
final int threadNum = i;
exec.execute(() -> {
try {
if (semaphore.tryAcquire()) { // 尝试获取一个许可
test(threadNum);
semaphore.release(); // 释放一个许可
}
} catch (Exception e) {
log.error("exception", e);
}
});
}
exec.shutdown();
}
private static void test(int threadNum) throws Exception {
log.info("{}", threadNum);
Thread.sleep(1000);
}
}
输出:
- 窗口数是3,来了20个人办业务,头3个人去的时候有空窗口,就办业务去了,办一次业务需要1天,剩下的17个人看窗口有人直接走了,所以输出只用3行;
18:30:24.027 [pool-1-thread-3] INFO com.example.concurrency.example.aqs.SemaphoreExample3 - 2
18:30:24.027 [pool-1-thread-1] INFO com.example.concurrency.example.aqs.SemaphoreExample3 - 0
18:30:24.027 [pool-1-thread-2] INFO com.example.concurrency.example.aqs.SemaphoreExample3 - 1
Semaphore示例(四) 尝试获取许可并限定等待时间
- 排队的时候给自己设置了一个等待时间,如果等了这么长时间了还没排到,就走;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@Slf4j
public class SemaphoreExample4 {
private final static int threadCount = 20;
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(3);
for (int i = 0; i < threadCount; i++) {
final int threadNum = i;
exec.execute(() -> {
try {
if (semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS)) { // 尝试获取一个许可
test(threadNum);
semaphore.release(); // 释放一个许可
}
} catch (Exception e) {
log.error("exception", e);
}
});
}
exec.shutdown();
}
private static void test(int threadNum) throws Exception {
log.info("{}", threadNum);
Thread.sleep(1000);
}
}
输出:
- 等一等,今天还是能办上的;
18:41:06.569 [pool-1-thread-3] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 2
18:41:06.569 [pool-1-thread-1] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 0
18:41:06.569 [pool-1-thread-2] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 1
18:41:07.578 [pool-1-thread-5] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 4
18:41:07.578 [pool-1-thread-4] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 3
18:41:07.578 [pool-1-thread-6] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 5
18:41:08.578 [pool-1-thread-9] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 8
18:41:08.578 [pool-1-thread-7] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 6
18:41:08.578 [pool-1-thread-8] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 7
18:41:09.578 [pool-1-thread-11] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 10
18:41:09.578 [pool-1-thread-10] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 9
18:41:09.578 [pool-1-thread-12] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 11
18:41:10.579 [pool-1-thread-15] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 14
18:41:10.579 [pool-1-thread-14] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 13
18:41:10.579 [pool-1-thread-13] INFO com.example.concurrency.example.aqs.SemaphoreExample4 - 12