闭锁CountDownLatch与栅栏CyclicBarrier

时间:2022-08-29 15:16:05
 
闭锁CountDownLatch与栅栏CyclicBarrier
 
 
浅谈 java 信号量 Semaphore
 
 
TestSemaphore testSemaphore = new TestSemaphore();
testSemaphore.exeAction();
 
/**
*
*/
protected class TestSemaphore {
 
 
/**
*
*/
public TestSemaphore() {
 
 
}
 
/**
*
*/
public void exeAction() {
List<ClaimAccident> claimAccidents = claimAccidentDao.findClaimAccident();
if (claimAccidents == null) {
throw new AppException("timingUpdateClaimAccidentItemStatus", "timingUpdateClaimAccidentItemStatus is null");
}
//构造线程池
ExecutorService executorService = new ThreadPoolExecutor(10, 20, 3000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(100));
//构造信号量 10
Semaphore semaphore = new Semaphore(10);
//模拟 claimAccidents.size()个顾客等待银行 10个窗口办理业务
for (int i = 1; i <= claimAccidents.size(); i++) {
final int claimitem = i;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
claimAccidents.forEach(claimAccident -> {
//queryAnlian
try {
ClaimProtection protection = claimAccident.getClaimProtection();
//query
IncOrder incOrder = Optional.ofNullable(incOrderDAO.findByOrderNo(protection.getOrderNo()))
.orElseThrow(() -> {
return new AppException("UNVALID ORDER", "unvalid order");
});
 
ClaimProtectionResponse response = claimProtectionOperation
.search(buildSearch(claimAccident, protection, incOrder));
log.info("INFO#timingUpdateClaimAccidentItemStatus - response: {}", apiCoreObjectMapper.writeValueAsString(response));
///result
if (!Objects.isNull(response.getBody()) && !Objects.isNull(response.getHead())) {
if (!Objects.isNull(response.getBody().getClaim())) {
 
ClaimProtectionClaim claim = response.getBody().getClaim();
//status
 
if (claim.getClaimList() != null && claim.getClaimList().size() > 0 && claim.getClaimList().get(0) != null && claim.getClaimList().get(0).getClaimStatusInfoList() != null &&
claim.getClaimList().get(0).getClaimStatusInfoList().size() > 0) {
String claimStatus = claim.getClaimList().get(0).getClaimStatusInfoList().get(0)
.getClaimStatus();
claimAccident.setClaimStatus(claimStatus);
ClaimStatus claimStatus1 = ClaimStatus.getInstance(claimStatus);
if (claimStatus1.getValue() == ClaimStatus.RECIVED.getValue()) {//已受理
claimAccident.setHzcStatus(HzcAccidentStatus.H5.getValue());
} else if (claimStatus1.getValue() == ClaimStatus.REVIEW.getValue()) { //审核中
claimAccident.setHzcStatus(HzcAccidentStatus.H6.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.DATA_UNOVER.getValue())) { //资料待补交
claimAccident.setHzcStatus(HzcAccidentStatus.H7.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.UNPAID.getValue())) { //保费未结
claimAccident.setHzcStatus(HzcAccidentStatus.H8.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.REFUND.getValue())) { //已退票
claimAccident.setHzcStatus(HzcAccidentStatus.H9.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.PAIED.getValue())) { //已支付
claimAccident.setHzcStatus(HzcAccidentStatus.H10.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.CANCELED.getValue())) { //已注销
claimAccident.setHzcStatus(HzcAccidentStatus.H11.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.REJECTED.getValue())) { //已拒赔
claimAccident.setHzcStatus(HzcAccidentStatus.H12.getValue());
} else if ((claimStatus1.getValue() == ClaimStatus.UNKOWN.getValue())) { //未知
claimAccident.setHzcStatus(HzcAccidentStatus.H99.getValue());
}
}
if (claim.getClaimList() != null && claim.getClaimList().size() > 0 && claim.getClaimList().get(0).getPayInfoList() != null && claim.getClaimList().get(0).getPayInfoList().size() > 0) {
BigDecimal payAmount = claim.getClaimList().get(0).getPayInfoList().get(0).getPayAmount();
claimAccident.setClaimPayAmount(payAmount);
}
 
//updatestatus
claimAccidentDao.save(claimAccident);
}
 
 
} else {
claimAccident.setErrorMessage(response.getHead().getErrorMessage());
claimAccidentDao.save(claimAccident);
}
} catch (Exception e) {
log.error("failed to get the response from #timingUpdateClaimAccidentItemStatus", e);
throw new AppException("", e.getMessage());
}
 
});
semaphore.release();
System.out.println("客户" + claimitem + "办理业务结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
executorService.submit(runnable);
}
//关闭线程池
executorService.shutdown();
}
 
 
}