转自:https://m.jb51.net/article/154421.htmjava
1 悲观锁redis
执行操做前假设当前的操做确定(或有很大概率)会被打断(悲观)。基于这个假设,咱们在作操做前就会把相关资源锁定,不容许本身执行期间有其余操做干扰。缓存
Redis不支持悲观锁。Redis做为缓存服务器使用时,以读操做为主,不多写操做,相应的操做被打断的概率较少。不采用悲观锁是为了防止下降性能。服务器
2 乐观锁分布式
执行操做前假设当前操做不会被打断(乐观)。基于这个假设,咱们在作操做前不会锁定资源,万一发生了其余操做的干扰,那么本次操做将被放弃。工具
3. Redis中的锁策略性能
Redis采用了乐观锁策略(经过watch操做)。乐观锁支持读操做,适用于多读少写的状况!
在事务中,能够经过watch命令来加锁;使用 UNWATCH能够取消加锁;
若是在事务以前,执行了WATCH(加锁),那么执行EXEC 命令或 DISCARD 命令后,锁对自动释放,即不须要再执行 UNWATCH 了学习
例子测试
redis锁工具类this
package com.fly.lock; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisLock { //初始化redis池 private static JedisPoolConfig config; private static JedisPool pool; static { config = new JedisPoolConfig(); config.setMaxTotal(30); config.setMaxIdle(10); pool = new JedisPool(config, "192.168.233.200", 6379); } /** * 给target上锁 * @param target **/ public static void lock(Object target) { //获取jedis Jedis jedis = pool.getResource(); //result接收setnx的返回值,初始值为0 Long result= 0L; while (result < 1) { //若是target在redis中已经存在,则返回0;不然,在redis中设置target键值对,并返回1 result = jedis.setnx(target.getClass().getName() + target.hashCode(), Thread.currentThread().getName()); } jedis.close(); } /** * 给target解锁 * @param target **/ public static void unLock(Object target) { Jedis jedis = pool.getResource(); //删除redis中target对象的键值对 Long del = jedis.del(target.getClass().getName() + target.hashCode()); jedis.close(); } /** * 尝试给target上锁,若是锁成功返回true,若是锁失败返回false * @param target * @return **/ public static boolean tryLock(Object target) { Jedis jedis = pool.getResource(); Long row = jedis.setnx(target.getClass().getName() + target.hashCode(), "true"); jedis.close(); if (row > 0) { return true; } return false; } }
测试类
package com.fly.test; import com.fly.lock.RedisLock; class Task { public void doTask() { //上锁 RedisLock.lock(this); System.out.println("当前线程: " + Thread.currentThread().getName()); System.out.println("开始执行: " + this.hashCode()); try { System.out.println("doing..."); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("完成: " + this.hashCode()); //解锁 RedisLock.unLock(this); } } public class Demo { public static void main(String[] args) { Task task = new Task(); Thread[] threads = new Thread[5]; for (Thread thread : threads) { thread = new Thread(()->{ task.doTask(); }); thread.start(); } } }
输出结果:
----------------------------------------------
当前线程: Thread-0
开始执行: 2081499965
doing...
完成: 2081499965
----------------------------------------------
当前线程: Thread-2
开始执行: 2081499965
doing...
完成: 2081499965
----------------------------------------------
当前线程: Thread-1
开始执行: 2081499965
doing...
完成: 2081499965
----------------------------------------------
当前线程: Thread-4
开始执行: 2081499965
doing...
完成: 2081499965
----------------------------------------------
当前线程: Thread-3
开始执行: 2081499965
doing...
完成: 2081499965
去掉redis锁后,执行结果:
----------------------------------------------
----------------------------------------------
当前线程: Thread-2
开始执行: 1926683415
----------------------------------------------
当前线程: Thread-1
doing...
当前线程: Thread-0
----------------------------------------------
当前线程: Thread-3
开始执行: 1926683415
doing...
开始执行: 1926683415
doing...
----------------------------------------------
开始执行: 1926683415
doing...
当前线程: Thread-4
开始执行: 1926683415
doing...
完成: 1926683415
完成: 1926683415
完成: 1926683415
完成: 1926683415
完成: 1926683415
Process finished with exit code 0
利用redis这个性质,能够实现分布式锁,固然设计必定复杂一些!
总结
以上就是这篇文章的所有内容了,但愿本文的内容对你们的学习或者工做具备必定的参考学习价值,谢谢你们对脚本之家的支持。若是你想了解更多相关内容请查看下面相关连接