java 并发模拟,简单重试 乐观锁控制 单元测试
@Test
public void testGoods() throws InterruptedException {
CountDownLatch main = new CountDownLatch(3);
CountDownLatch countDownLatch = new CountDownLatch(3);
// 回退库存
for (int i = 0; i < 3 ; i++) {
countDownLatch.countDown();
new Thread(new Runnable() {
@SneakyThrows
@Override
public void run() {
countDownLatch.await();
// 简单重试
for (int j = 0; j < 3; j++) {
try {
testReduce();
break;
} catch (Exception e) {
System.out.println("重试"+(j+1)+"减库存");
if(j==2){
System.out.println("重试"+(j+1)+"减库存失败");
}
}
}
main.countDown();
}
}).start();
}
main.await();
}
elk