//设置信号量的容量为10,超过10后,则在队列排队。
//tryAcquire()尝试向信号器获取一个信号量
//release()释放信号量
static final int productThreadNum=10;
static final int productTimeOut=50;
static final int productQueueLenth=1000;
static final Semaphore productSemaphore=new Semaphore(productThreadNum,true);
public static boolean tryAcquireProduct() throws InterruptedException{
int count=productSemaphore.getQueueLength();
log.info("当前等待线程:{}",count);
if(count>productQueueLenth){
throw new LogicalException("购买人数过多,请稍后重试");
}
return productSemaphore.tryAcquire(productTimeOut, TimeUnit.SECONDS);
}
public static void releaseProuct(){
productSemaphore.release();
}
https://github.com/jerrik123
https://gitee.com/huangyong