1、tryLock比较容易返回false,导致失败线程多
@GetMapping("/test")
public String testLock() {
String result = “”;
RLock lock = redissonClient.getLock(“test”);
boolean b = true;
// 尝试加锁,最多等待20秒
try {
b = lock.tryLock(20, TimeUnit.SECONDS);
if (b) {
System.out.println(“进入方法”);
Object test = map.get(“test”);
if (test == null) {
map.put(“test”, 0);
}
Integer i = (Integer) map.get(“test”);
Thread.sleep(200);
i++;
map.put(“test”, i);
System.out.println(Thread.currentThread().getId() + ": " + i);
result = “success”;
} else {
result = “false”;
System.out.println(“未能拿到锁”);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(“我释放了锁”);
if (b) {
<
redisson分布式锁
最新推荐文章于 2024-05-16 17:22:30 发布