併發模擬-程式碼CountDownLatch,Semaphore
CountDownLatch
Semaphore
控制同一時間併發量
import java.util.concurrent.Semaphore;
public class AccessControlService {
public static class ConcurrentLimitException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
private static final int MAX_PERMITS = 100;
private Semaphore permits = new Semaphore(MAX_PERMITS, true);
public boolean login(String name, String password) {
if (!permits.tryAcquire()) {
// 同時登入使用者數超過限制
throw new ConcurrentLimitException();
}
// ..其他驗證
return true;
}
public void logout(String name) {
permits.release();
}
}
用一下
import com.mmall.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@Slf4j
@ThreadSafe
public class CollectionsExample3 {
// 請求總數
public static int clientTotal = 5000;
// 同時併發執行的執行緒數
public static int threadTotal = 200;
private static Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap<>());
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
final int count = i;
executorService.execute(() -> {
try {
semaphore.acquire();
update(count);
semaphore.release();
} catch (Exception e) {
log.error("exception", e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
log.info("size:{}", map.size());
}
private static void update(int i) {
map.put(i, i);
}
}
相關文章
- Java併發(6)- CountDownLatch、Semaphore與AQSJavaCountDownLatchAQS
- 併發-6-wait、notify、Semaphore、CountDownLatch、CyclicBarrierAICountDownLatch
- Java 併發工具類 CountDownLatch、CyclicBarrier、Semaphore、ExchangerJavaCountDownLatch
- 07 併發工具類CountDownLatch、CyclicBarrier、Semaphore使用及原始碼分析CountDownLatch原始碼
- JUC併發程式設計之Semaphore、CountDownLatch、CyclicBarrier協同工具程式設計CountDownLatch
- 【高併發】AQS中的CountDownLatch、Semaphore與CyclicBarrier用法總結AQSCountDownLatch
- Java使用程式碼模擬高併發操作Java
- java多執行緒10:併發工具類CountDownLatch、CyclicBarrier和SemaphoreJava執行緒CountDownLatch
- Java 併發程式設計(十三) -- CountDownLatch原始碼分析Java程式設計CountDownLatch原始碼
- 併發程式設計之:CountDownLatch程式設計CountDownLatch
- Java 併發程式設計(十五) -- Semaphore原始碼分析Java程式設計原始碼
- Redisson 分散式鎖原始碼 11:Semaphore 和 CountDownLatchRedis分散式原始碼CountDownLatch
- Java併發包5--同步工具CountDownLatch、CyclicBarrier、Semaphore的實現原理解析JavaCountDownLatch
- 突擊併發程式設計JUC系列-併發工具 Semaphore程式設計
- 併發工具類:Semaphore原始碼解讀原始碼
- Java併發之Semaphore原始碼解析(一)Java原始碼
- Java併發之Semaphore原始碼解析(二)Java原始碼
- 併發工具類——Semaphore
- JUC併發系列(八):併發程式設計常用輔助類CountDownLatch與CyclicBarrier(手敲程式碼示例)程式設計CountDownLatch
- Java併發—— CountDownLatch與CyclicBarrierJavaCountDownLatch
- CyclicBarrier、CountDownLatch以及Semaphore使用及其原理分析CountDownLatch
- CountDownLatch、CyclicBarrier、Semaphore、Exchanger 的詳細解析CountDownLatch
- Java併發程式設計系列之Semaphore詳解Java程式設計
- JUC原始碼學習筆記2——AQS共享和Semaphore,CountDownLatch原始碼筆記AQSCountDownLatch
- Java 併發包之CountDownLatch、CyclicBarrierJavaCountDownLatch
- 高併發之ReentrantLock、CountDownLatch、CyclicBarrierReentrantLockCountDownLatch
- Java併發系列—工具類:SemaphoreJava
- 高併發之Semaphore、Exchanger、LockSupport
- 模擬併發及生成唯一
- Laravel 高併發搶購模擬Laravel
- 漫畫:Java併發閉鎖CountDownLatchJavaCountDownLatch
- Java併發包原始碼學習系列:同步元件CountDownLatch原始碼解析Java原始碼元件CountDownLatch
- 面試官:說說CountDownLatch,CyclicBarrier,Semaphore的原理?面試CountDownLatch
- Java併發包原始碼學習系列:同步元件Semaphore原始碼解析Java原始碼元件
- Redis 快取穿透,擊穿解決方案,模擬高併發請求,附程式碼Redis快取穿透
- 大白話說java併發工具類-CountDownLatch,CyclicBarrierJavaCountDownLatch
- 2024.9.28 程式碼源模擬賽
- 強大的三個工具類、CountDownLatch 閉鎖 、CyclicBarrier 、SemaphoreCountDownLatch