• redis 加锁与释放锁(分布式锁1)


    使用Redis的 SETNX 命令可以实现分布式锁

    SETNX key value

    返回值

    返回整数,具体为 
    - 1,当 key 的值被设置 
    - 0,当 key 的值没被设置

    分布式锁使用

    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    /**
     * @Author: feng
     * @Date: 2019/5/25 21:54
     * @Description:
     */
    public class RedisManager {
    
        private static JedisPool jedisPool;
    
        static {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(18);
            jedisPoolConfig.setMaxIdle(5);
            jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379,5000);
        }
    
        public static Jedis getJedis() throws Exception {
            if (null != jedisPool){
                return jedisPool.getResource();
            }
            throw new Exception("Jedispool was not init");
        }
    }
    

      

    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.Transaction;
    
    import java.util.List;
    
    /**
     * @Author: feng
     * @Date: 2019/5/25 22:04
     * @Description:
     */
    public class RedisLock {
    
        public String getLock(String key, int timeout) {
            Jedis jedis = null;
            try {
                jedis = RedisManager.getJedis();
    //            String value = UUID.randomUUID().toString();
                //以当前线程id作为value
                String value = String.valueOf(Thread.currentThread().getId());
                long end = System.currentTimeMillis() + timeout;
                while (System.currentTimeMillis() < end) {
                    if (jedis.setnx(key, value) == 1) {
                        jedis.expire(key, timeout);
                        return value;
                    }
                    if (jedis.ttl(key) == -1) {
                        jedis.expire(key, timeout);
                    }
                    Thread.sleep(500);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                jedis.close();
            }
            return null;
        }
    
        public boolean releaseLock(String key, String value) {
            Jedis jedis = null;
            try {
                jedis = RedisManager.getJedis();
                while (true) {
                    jedis.watch(key);
                    System.out.println(jedis.get(key));
                    if (value.equals(jedis.get(key))) {
                        Transaction transaction = jedis.multi();
                        transaction.del(key);
                        List<Object> list = transaction.exec();
                        if (list == null) {
                            continue;
                        }
                    }
                    jedis.unwatch();
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                jedis.close();
            }
            return false;
        }
    
        public static void main(String[] args) {
            RedisLock redisLock = new RedisLock();
            String thredId = redisLock.getLock("test", 1000);
            System.out.println("返回值value:" + thredId);
            boolean b = redisLock.releaseLock("test", String.valueOf(Thread.currentThread().getId()));
            System.out.println(b);
        }
    
    }
    

      

  • 相关阅读:
    注意 定义类的时候 不要null 别忘了new 实例化 否则报错
    如果通过key获取dictionary里面的value
    C#邮箱身份验证
    时间选择器 可以选择日期和时间
    C#的Timer解析
    使用微软的组件发邮件
    将html代码写入临时文件夹下面的 然后发来给webbrowser使用
    c#中DataGridView 如何设置 才能选中一行 设置鼠标事件
    lumisoft 获取邮件的方法
    C# 动态添加CheckedListBox的选项,并设置选项为 选中? 怎么做?
  • 原文地址:https://www.cnblogs.com/Andrew520/p/10923903.html
Copyright © 2020-2023  润新知