併發模擬-程式碼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併發程式設計:CountDownLatch、CyclicBarrier和SemaphoreJava程式設計CountDownLatch
- Java併發(6)- CountDownLatch、Semaphore與AQSJavaCountDownLatchAQS
- Java併發之CountDownLatch、CyclicBarrier和SemaphoreJavaCountDownLatch
- Java 併發工具類 CountDownLatch、CyclicBarrier、Semaphore、ExchangerJavaCountDownLatch
- 07 併發工具類CountDownLatch、CyclicBarrier、Semaphore使用及原始碼分析CountDownLatch原始碼
- JUC併發程式設計之Semaphore、CountDownLatch、CyclicBarrier協同工具程式設計CountDownLatch
- 併發-6-wait、notify、Semaphore、CountDownLatch、CyclicBarrierAICountDownLatch
- 【高併發】AQS中的CountDownLatch、Semaphore與CyclicBarrier用法總結AQSCountDownLatch
- Java使用程式碼模擬高併發操作Java
- Oracle模擬併發指令碼Oracle指令碼
- java多執行緒10:併發工具類CountDownLatch、CyclicBarrier和SemaphoreJava執行緒CountDownLatch
- 併發程式設計之:CountDownLatch程式設計CountDownLatch
- Java 併發程式設計(十三) -- CountDownLatch原始碼分析Java程式設計CountDownLatch原始碼
- Java 併發程式設計(十五) -- Semaphore原始碼分析Java程式設計原始碼
- Java併發包5--同步工具CountDownLatch、CyclicBarrier、Semaphore的實現原理解析JavaCountDownLatch
- Redisson 分散式鎖原始碼 11:Semaphore 和 CountDownLatchRedis分散式原始碼CountDownLatch
- 突擊併發程式設計JUC系列-併發工具 Semaphore程式設計
- Java併發之Semaphore原始碼解析(一)Java原始碼
- Java併發之Semaphore原始碼解析(二)Java原始碼
- 併發工具類:Semaphore原始碼解讀原始碼
- 併發工具類——Semaphore
- 模擬web高併發Web
- CyclicBarrier、CountDownLatch與Semaphore的小記CountDownLatch
- Java併發程式設計系列之Semaphore詳解Java程式設計
- JUC併發系列(八):併發程式設計常用輔助類CountDownLatch與CyclicBarrier(手敲程式碼示例)程式設計CountDownLatch
- Java併發—— CountDownLatch與CyclicBarrierJavaCountDownLatch
- CountDownLatch、CyclicBarrier、Semaphore、Exchanger 的詳細解析CountDownLatch
- CyclicBarrier、CountDownLatch以及Semaphore使用及其原理分析CountDownLatch
- Java併發程式設計實戰--閉鎖 CountDownLatchJava程式設計CountDownLatch
- JUC原始碼學習筆記2——AQS共享和Semaphore,CountDownLatch原始碼筆記AQSCountDownLatch
- Laravel 高併發搶購模擬Laravel
- Java 併發包之CountDownLatch、CyclicBarrierJavaCountDownLatch
- Java併發系列—工具類:SemaphoreJava
- 面試官:說說CountDownLatch,CyclicBarrier,Semaphore的原理?面試CountDownLatch
- 漫畫:Java併發閉鎖CountDownLatchJavaCountDownLatch
- 高併發之ReentrantLock、CountDownLatch、CyclicBarrierReentrantLockCountDownLatch
- Java併發工具類(閉鎖CountDownLatch)JavaCountDownLatch
- 模擬發放紅包效果程式碼例項