package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class RedisLock { public static void main(String[] args) { JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
// 从连接池中获取一个jedis对象
Jedis jedis = jedisPool.getResource(); //简单加redis分布式锁的方式
boolean lock = false;
String key = "mylock";
String value = System.currentTimeMillis()+""; try {
//50秒自动解锁
String setResult = jedis.set(key,value , "nx", "ex", 50L);
long startTime = 0;
if (setResult!=null) {
lock = true;
startTime = System.currentTimeMillis();
} //没有获取到锁,退出
if (!lock) {
return;
} //执行到了这一步说明已经获取到锁
// doSth long endTime = System.currentTimeMillis();
//如果超时解锁
if ((endTime-startTime)>5000) {
//打印日志
System.out.println("出现超时解锁-----------key:"+key);
} } catch (Exception e) {
e.printStackTrace();
}finally {
//这里是为了避免超时后,释放掉其他线程的同名锁
//但是有个问题,就是下面的代码不是原子操作,可能会有问题,这里忽略
String s = jedis.get(key);
if (value.equals(s)) {
//释放锁
jedis.del(key);
}
} }
}
package redis;

import java.util.HashMap;
import java.util.Map; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class RedisReentrantLock { private ThreadLocal<Map<String,Integer>> lockers = new ThreadLocal<>();
private Jedis jedis; public RedisReentrantLock(Jedis jedis) {
super();
this.jedis = jedis;
} private boolean _lock(String key) {
return jedis.set(key,"" , "nx", "ex", 5L)!=null;
} private void _unlock(String key) {
jedis.del(key);
} private Map<String, Integer> currentLockers(){
Map<String, Integer> refs = lockers.get();
if (refs !=null) {
return refs;
}
lockers.set(new HashMap<String, Integer>());
return lockers.get();
} public boolean lock(String key) {
Map<String, Integer> refs = currentLockers();
Integer refCnt = refs.get(key);
if (refCnt!=null) {
refs.put(key, refCnt+1);
return true;
}
boolean ok = this._lock(key);
if (!ok) {
return false;
} refs.put(key, 1);
return true;
} public boolean unlock(String key) {
Map<String, Integer> refs = currentLockers();
Integer refCnt = refs.get(key);
if (refCnt == null) {
return false;
}
refCnt -= 1;
if (refCnt>0) {
refs.put(key, refCnt);
}else {
refs.remove(key);
this._unlock(key);
}
return true;
} public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
// 从连接池中获取一个jedis对象
Jedis jedis = jedisPool.getResource();
RedisReentrantLock redisLock = new RedisReentrantLock(jedis);
System.out.println(redisLock.lock("lock1"));
System.out.println(redisLock.lock("lock1"));
System.out.println(redisLock.unlock("lock1"));
System.out.println(redisLock.unlock("lock1"));
} }

redis分布式锁练习【我】的更多相关文章

  1. 利用redis分布式锁的功能来实现定时器的分布式

    文章来源于我的 iteye blog http://ak478288.iteye.com/blog/1898190 以前为部门内部开发过一个定时器程序,这个定时器很简单,就是配置quartz,来实现定 ...

  2. Redis分布式锁

    Redis分布式锁 分布式锁是许多环境中非常有用的原语,其中不同的进程必须以相互排斥的方式与共享资源一起运行. 有许多图书馆和博客文章描述了如何使用Redis实现DLM(分布式锁管理器),但是每个库都 ...

  3. redis分布式锁和消息队列

    最近博主在看redis的时候发现了两种redis使用方式,与之前redis作为缓存不同,利用的是redis可设置key的有效时间和redis的BRPOP命令. 分布式锁 由于目前一些编程语言,如PHP ...

  4. redis咋么实现分布式锁,redis分布式锁的实现方式,redis做分布式锁 积极正义的少年

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...

  5. spring boot redis分布式锁

    随着现在分布式架构越来越盛行,在很多场景下需要使用到分布式锁.分布式锁的实现有很多种,比如基于数据库. zookeeper 等,本文主要介绍使用 Redis 做分布式锁的方式,并封装成spring b ...

  6. Redis分布式锁的正确实现方式

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...

  7. Redis分布式锁---完美实现

    这几天在做项目缓存时候,因为是分布式的所以需要加锁,就用到了Redis锁,正好从网上发现两篇非常棒的文章,来和大家分享一下. 第一篇是简单完美的实现,第二篇是用到的Redisson. Redis分布式 ...

  8. redis分布式锁实践

    分布式锁在多实例部署,分布式系统中经常会使用到,这是因为基于jvm的锁无法满足多实例中锁的需求,本篇将讲下redis如何通过Lua脚本实现分布式锁,不同于网上的redission,完全是手动实现的 我 ...

  9. Redis分布式锁的try-with-resources实现

    Redis分布式锁的try-with-resources实现 一.简介 在当今这个时代,单体应用(standalone)已经很少了,java提供的synchronized已经不能满足需求,大家自然 而 ...

  10. 关于分布式锁原理的一些学习与思考-redis分布式锁,zookeeper分布式锁

    首先分布式锁和我们平常讲到的锁原理基本一样,目的就是确保,在多个线程并发时,只有一个线程在同一刻操作这个业务或者说方法.变量. 在一个进程中,也就是一个jvm 或者说应用中,我们很容易去处理控制,在j ...

随机推荐

  1. EtherNet/IP 协议应用层使用CIP协议&CIP协议中使用的TLS和DTLS(Network Infrastructure for EtherNet/IPTM: Introduction and Considerations)

  2. [Selenium3+python3.6]自动化测试3-八种元素元素定位(Firebug和firepath)

    参考http://www.cnblogs.com/yoyoketang/p/6123890.html   #coding=utf-8 from selenium import webdriverdri ...

  3. python之新的开始

    Day 1-Morning     终于开通了新的博客(等待审核的过程用着备忘录敲...)~感谢几位大佬们愿意带我一起学习 大家一起加油!(苟富贵,勿相忘!/doge 哈哈哈) 初学python,以下 ...

  4. Python使用selenium模拟点击(二)

    本篇文章是接着第一篇文章讲的 具体可看第一篇:https://www.cnblogs.com/whatarey/p/10477754.html 要实现功能>搜索完毕,自动点击 这个功能做的停操蛋 ...

  5. webpack中使用WebpackDevServer实现请求转发

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  6. Reservoir Sampling 蓄水池采样算法

    https://blog.csdn.net/huagong_adu/article/details/7619665 https://www.jianshu.com/p/63f6cf19923d htt ...

  7. Java8-Lock-No.03

    import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutorService; import ...

  8. 2019牛客多校C Governing sand——桶&&思维题

    题意 有 $n$ 种树,每种树都有高度 $H_i$,费用 $C_i$,数量 $P_i$,现要砍掉一些树,使得剩下的树中最高的树的数量超过一般,求最小的费用.($1 \leq n \leq 10^5, ...

  9. Qt 把连续两次单击当成双击

    方法1: 主要通过计时器,在一段时间内的连续两次单击,触发双击事件 void init() { m_nClickCount = 0; m_timer = new QTimer(this); conne ...

  10. 9、Spring Boot 2.x 集成 Thymeleaf

    1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...