码迷,mamicode.com
首页 > 其他好文 > 详细

Redis 实现锁

时间:2015-05-27 18:47:19      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

 1 public boolean tryLock2(String key, int timeout, int expiretime, int sleeptime) throws Exception {
 2 
 3         Jedis redis = jedisPool.getResource();
 4         try {
 5             long nano = System.nanoTime();
 6             do {
 7                 Long i = redis.setnx(key, "key");
 8                 jedisPool.returnResource(redis);
 9                 if (i == 1) {
10                     redis.expire(key, expiretime);
11                     return Boolean.TRUE;
12                 }
13                 if (timeout == 0) {
14                     break;
15                 }
16                 Thread.sleep(sleeptime);
17             } while ((System.nanoTime() - nano) < TimeUnit.SECONDS.toNanos(timeout));
18             return Boolean.FALSE;
19         } catch (RuntimeException | InterruptedException e) {
20             if (redis != null) {
21                 jedisPool.returnBrokenResource(redis);
22             }
23             throw e;
24         }
25     }
26 
27     public boolean tryLock(String key, int timeout, int expiretime, int sleeptime) throws Exception {
28 
29         Jedis redis = jedisPool.getResource();
30         try {
31             long nano = System.nanoTime();
32 
33             do {
34                 long timestamp = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiretime) + 1;
35                 Long i = redis.setnx(key, String.valueOf(timestamp));
36                 jedisPool.returnResource(redis);
37                 if (i == 1) {
38                     return Boolean.TRUE;
39                 }
40                 String lockVal = getString(key);
41                 if (StringUtils.isBlank(lockVal) || !StringUtils.isNumeric(lockVal)) {
42                     lockVal = "0";
43                 }
44                 if (System.currentTimeMillis() > Long.valueOf(lockVal)) {
45                     lockVal = getAndset(key, String.valueOf(timestamp));
46                     if (StringUtils.isBlank(lockVal) || !StringUtils.isNumeric(lockVal)) {
47                         lockVal = "0";
48                     }
49                     if (System.currentTimeMillis() > Long.valueOf(lockVal)) {
50                         return Boolean.TRUE;
51                     }
52                 }
53                 if (timeout == 0) {
54                     break;
55                 }
56                 Thread.sleep(sleeptime);
57             } while ((System.nanoTime() - nano) < TimeUnit.SECONDS.toNanos(timeout));
58             return Boolean.FALSE;
59         } catch (RuntimeException | InterruptedException e) {
60             if (redis != null) {
61                 jedisPool.returnBrokenResource(redis);
62             }
63             throw e;
64         }
65     }

 

Redis 实现锁

标签:

原文地址:http://www.cnblogs.com/ryhan/p/4534046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!