1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
@RequestMapping("lock") public void lock() { RLock lock = redissonClient.getLock("lock"); try { lock.lock(3, TimeUnit.SECONDS); System.out.println("lock..."); String s = redisTemplate.opsForValue().get("num"); if (s != null) { int num = Integer.parseInt(s); num ++; redisTemplate.opsForValue().set("num", Integer.toString(num)); } } finally { if (lock.isHeldByCurrentThread() && lock.isLocked()) { lock.unlock(); } System.out.println("unlock..."); } }
|