package com.example.sina.thread;
import java.time.LocalDateTime;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SemaphoreTest {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(10);
ThreadPoolExecutor semaphoreThread = new ThreadPoolExecutor(10, 50, 10, TimeUnit.SECONDS,
new LinkedBlockingQueue<>());
for (int i = 0; i < 50; i++) {
semaphoreThread.execute(() -> {
try {
// 堵塞获取许可
semaphore.acquire();
System.out.println("Thread:" + Thread.currentThread().getName() + " 时间:" + LocalDateTime.now());
TimeUnit.SECONDS.sleep(1);
// 释放许可
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
semaphoreThread.shutdown();
}
}