springbootでのsemaphoreの同期操作を記録する
JAva独自のSemaphoreクラス
出力結果:
private Semaphore semaphore = new Semaphore(1);
@SneakyThrows
@RequestMapping(value = "/02",method = RequestMethod.GET)
public void test2(){
int id = semaphore.availablePermits();
log.info("=========== {}",id);
if(id > 0){
log.info("=========== ");
try {
semaphore.acquire(1);
log.info("=========== ");
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
semaphore.release(1);
log.info("=========== ");
}
}else {
log.info("=========== , ");
}
}
出力結果:
=========== 1
===========
===========
=========== 0
=========== ,
=========== 0
=========== ,
=========== 0
=========== ,
=========== 0
=========== ,
=========== 0
=========== ,
=========== 0
=========== ,
===========