1.设置超时时间,即使获取到锁后,客户端挂掉了,一定时间后锁释放。不会出现死锁。
2.每个锁对象都有一个自己的uuid作为redis 的value。当释放锁的时候,先去判断当前key对应的value是否还是本是当前对象的value.(但是本实例中没有这个场景)
3.防止操作阻塞超过了超时,本实例中会不断的重置超时时间。如果锁释放,重置超时会返回0,则中断循环。但是不断重置超时可能会造成阻塞,可以设置重置超时次数。
4.为了保证原子性,使用redis的lua脚本
1.RedisTool
public class RedisTool {
private static Jedis jedis = getJedis();
private static Jedis getJedis() {
JedisPoolConfig config = new JedisPoolConfig();
/*配置连接池最大空闲数*/
config.setMaxIdle(50);
/*配置连接池最大连接数*/
config.setMaxTotal(100);
/*最大等待毫秒数*/
config.setMaxWaitMillis(20000);
/*使用配置创建连接池*/
JedisPool jedisPool = new JedisPool(config, "localhost");
/*从连接池中获取单个链接*/
Jedis jedis = jedisPool.getResource();
return jedis;
}
public static Jedis getJedisInstance() {
return jedis;
}
}
2.RedisLock
public class RedisLock {
String lockKey;
String value = UUID.randomUUID().toString();
Jedis jedis;
int timeOut;
boolean isOpen = true;
public RedisLock(String lockKey, Jedis jedis, int timeOut) {
this.jedis = jedis;
this.lockKey = lockKey;
this.timeOut = timeOut;
}
public void lock() {
while (true) {
if (tryLock()) {
break;
}
}
}
public void lockInterrupt() throws InterruptedException {
while (true) {
boolean interrupted = Thread.currentThread().isInterrupted();
if (interrupted) {
throw new InterruptedException("中断");
}
if (tryLock()) {
break;
}
}
}
public boolean tryLock() {
String result = jedis.set(lockKey,value,"NX","EX",timeOut);
if ("OK".equals(result)) {
System.out.println("线程id:"+Thread.currentThread().getId() + "加锁成功!时间:"+System.currentTimeMillis());
if(isOpen) {
new Thread(new growthTimeout()).start();
}
return true;
}else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return false;
}
}
return false;
}
private class growthTimeout implements Runnable {
@Override
public void run() {
while (true) {
String ctl = "if redis.call('get',KEYS[1]) == ARGV[1] then " +
"return redis.call('expire',KEYS[1],ARGV[2]) " +
"else " +
"return 0 end";
Object re = jedis.eval(ctl,1,lockKey,value,"3000");
if("0".equals(re.toString())) {
break;
}else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
}
}
}
public void unLock() {
String script = "if redis.call('get',KEYS[1]) == ARGV[1] then " +
"return redis.call('del',KEYS[1]) " +
"else " +
"return 0 end";
jedis.eval(script,1,lockKey,value);
System.out.println("线程id:"+Thread.currentThread().getId() + "解锁成功!时间"+System.currentTimeMillis());
}
}
3.测试类
public class MyTest {
public static void main(String[] args) throws InterruptedException {
Jedis jedis = RedisTool.getJedisInstance();
RedisLock lock = new RedisLock("lcc",jedis,1000);
new Thread(() ->{
lock.lock();
try {
System.out.println("lock1");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.unLock();
}).start();
Thread.sleep(500);
/*调用克中断lock方法*/
Thread tt = new Thread(() ->{
try {
lock.lockInterrupt();
System.out.println("lock2");
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.unLock();
});
tt.start();
tt.interrupt();
}
}
GitHub:https://github.com/liucc0413/Redis/tree/master/src/main/java/com/redis/demo/DistributedJedisLock