jedis使用分布式锁

import redis.clients.jedis.Jedis;

public class A {
public static void main(String[] args) throws Exception {
Jedis jedis = new Jedis("localhost", 6379);
String set = jedis.set("key1", "value1", "NX", "PX", 5000);
      //NX->不存在才进行set PX->毫秒 EX->秒
      //set成功返回ok 失败返回null
System.out.println(set);

for (int i = 0; i < 10; i++) {
System.out.println(i);
String key1 = jedis.get("key1");
System.err.println(key1);
Thread.sleep(2000);
}
}
}

可避免
try {
jedis.setnx("key1", "value1");
int i = 1/0;
jedis.expire("key1", 5);
} catch (Exception e){
e.printStackTrace();
}

此方式造成的死锁问题~~
原文地址:https://www.cnblogs.com/bzdofj/p/14219657.html