Redis分布式锁

在集群等多服务器(意味着多个jvm)中经常要使用到同步处理一下业务,这时普通的事务是满足不要业务需求,需要分布式锁。分布式锁的实现方式有多种,如redis实现分布式锁,zookeeper实现分布式锁等,这篇先实现redis分布式锁。

实现原理

1、通过setnx(lock_timeout)实现,如果设置了锁返回1,已经有值没有设置成功返回0。

2、死锁问题:通过时间来判断是否过期,如果已经过期,获取到过期时间get(lockKey),然后getset(lock_timeout)判断是否和get相同,相同则证明已经加锁成功,因为可能会导致多个线程同时执行getset(lock_timeout)方法。这是可能导致多个线程都只需getset后,对于判断加锁成功的线程,再加expire(lockKey, LOCK_TIMEOUT, TimeUnit.MILLISECONDS)过期时间,防止多个线程同时叠加时间,导致锁时效时间翻倍。

3、针对集群服务器时间不一致问题,可以从调用redis的time()获取当前时间。

组件依赖

使用SpringBoot集成时的依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <version>1.5.4.RELEASE</version>
    </dependency>

使用Spring MVC(带有Spring Session Redis)时的依赖

    <dependency>
       <groupId>org.springframework.session</groupId>
       <artifactId>spring-session-data-redis</artifactId>
       <version>1.3.1.RELEASE</version>
   </dependency>

使用Spring MVC(不带有Spring Session Redis)时的依赖

    <dependency>  
      <groupId>org.springframework.data</groupId>  
      <artifactId>spring-data-redis</artifactId>  
      <version>1.0.2.RELEASE</version>  
    </dependency> 
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

代码实现

分布式锁接口

public interface DistributionLock {
    //加锁成功 返回加锁时间
    Long lock(String lockKey, String threadname);
    //解锁 需要更加加锁时间判断是否有权限
    void unlock(String lockKey, long lockvalue, String threadname);
}

分布式锁实现

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

@Component
public class RedisDistributionLock implements DistributionLock {
    private static final long LOCK_TIMEOUT = 60 * 1000; //加锁超时时间 单位毫秒  意味着加锁期间内执行完操作 如果未完成会有并发现象

    private static final Logger LOG = Logger.getLogger(RedisDistributionLock.class); //redis锁日志


    private RedisTemplate<String, Object> redisTemplate;


    RedisDistributionLock(RedisTemplate<String, Object> redisTemplate){
        this.redisTemplate = redisTemplate;
    }

//    @SuppressWarnings("unchecked")
//    private static RedisTemplate<Serializable, Serializable> redisTemplate = (RedisTemplate<Serializable, Serializable>) SpringContextHolder
//            .getBean("redisTemplate");

    /**
     * 取到锁加锁 取不到锁一直等待直到获得锁
     */
    @Override
    public Long lock(String lockKey, String threadname) {
        LOG.info(threadname + "开始执行加锁");
        while (true) { //循环获取锁
            Long lock_timeout = currentTimeFromRedis() + LOCK_TIMEOUT + 1; //锁时间

            if (redisTemplate.execute(new RedisCallback<Boolean>() {
                @Override
                public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                    JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
                    byte[] value = jdkSerializer.serialize(lock_timeout);
                    return connection.setNX(lockKey.getBytes(), value);
                }
            })) { //如果加锁成功
                LOG.info(threadname + "加锁成功++++++++111111111");
                redisTemplate.expire(lockKey, LOCK_TIMEOUT, TimeUnit.MILLISECONDS); //设置超时时间,释放内存
                return lock_timeout;
            }else {
                Long current_lock_timeout_Str = (Long) get(lockKey); // redis里的时间
                if (current_lock_timeout_Str != null && current_lock_timeout_Str < currentTimeFromRedis()) { //锁已经失效
                    // 判断是否为空,不为空的情况下,说明已经失效,如果被其他线程设置了值,则第二个条件判断是无法执行

                    Long old_lock_timeout_Str = (Long) getAndSet(lockKey, lock_timeout);
                    // 获取上一个锁到期时间,并设置现在的锁到期时间
                    if (old_lock_timeout_Str != null && old_lock_timeout_Str.equals(current_lock_timeout_Str)) {
                        // 如过这个时候,多个线程恰好都到了这里,但是只有一个线程的设置值和当前值相同,他才有权利获取锁
                        LOG.info(threadname + "加锁成功+++++++2222222222");
                        redisTemplate.expire(lockKey, LOCK_TIMEOUT, TimeUnit.MILLISECONDS); //设置超时时间,释放内存
                        return lock_timeout;//返回加锁时间
                    }
                }
            }

            try {
                LOG.info(threadname +  "等待加锁,睡眠100毫秒");
                TimeUnit.MILLISECONDS.sleep(100);//睡眠100毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private Object get(final String key) {
        Object obj = null;
        try {
            obj = redisTemplate.execute(new RedisCallback<Object>() {
                @Override
                public Object doInRedis(RedisConnection connection) throws DataAccessException {
//                    StringRedisSerializer serializer = new StringRedisSerializer();
                    JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
                    byte[] data = connection.get(key.getBytes());
                    connection.close();
                    if (data == null) {
                        return null;
                    }
                    return jdkSerializer.deserialize(data);
                }
            });
        } catch (Exception e) {
            LOG.error("get redis error", e);
        }
        return obj;
    }

    private Object getAndSet(final String key, final Long value) {
        Object obj = null;
        try {
            obj = redisTemplate.execute(new RedisCallback<Object>() {
                @Override
                public Object doInRedis(RedisConnection connection) throws DataAccessException {
//                    StringRedisSerializer serializer = new StringRedisSerializer();
                    JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
                    byte[] ret = connection.getSet(key.getBytes(), jdkSerializer.serialize(value));
                    connection.close();
                    return jdkSerializer.deserialize(ret);
                }
            });
        } catch (Exception e) {
            LOG.error("setNX redis error", e);
        }
        return obj;
    }

    @Override
    public void unlock(String lockKey, long lockvalue, String threadname) {
        LOG.info(threadname + "执行解锁==========");//正常直接删除 如果异常关闭判断加锁会判断过期时间
        Long current_lock_timeout_Str = (Long) get(lockKey); // redis里的时间
        if (current_lock_timeout_Str != null && current_lock_timeout_Str == lockvalue) {//如果是加锁者 则删除锁 如果不是则等待自动过期 重新竞争加锁
            redisTemplate.delete(lockKey); //删除键
            LOG.info(threadname + "解锁成功-----------------");
        }
    }


    public long currentTimeFromRedis(){ //获取redis当前时间
        return redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.time();
            }
        });
    }
}

模拟测试

测试代码

    public void testRedisDistributionLock(){

        for (int i = 0; i < 20; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String name = Thread.currentThread().getName();
                    Long locktime; //加锁时间
                    if ((locktime = distributionLock.lock("distribution123456", name)) != null) {
                        //开始执行任务
                        System.out.println(name + "任务执行中");
                        //任务执行完毕 关闭锁
                        distributionLock.unlock("distribution123456", locktime, name);
                    }
                }
            }).start();
        }

    }

测试结果

2018-02-06 12:13:20.946  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15开始执行加锁
2018-02-06 12:13:20.946  INFO 11719 --- [      Thread-18] com.sn.RedisLock.RedisDistributionLock   : Thread-18开始执行加锁
2018-02-06 12:13:20.946  INFO 11719 --- [      Thread-16] com.sn.RedisLock.RedisDistributionLock   : Thread-16开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21开始执行加锁
2018-02-06 12:13:20.946  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24开始执行加锁
2018-02-06 12:13:20.946  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23开始执行加锁
2018-02-06 12:13:20.948  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31开始执行加锁
2018-02-06 12:13:20.948  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25开始执行加锁
2018-02-06 12:13:20.948  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-26] com.sn.RedisLock.RedisDistributionLock   : Thread-26开始执行加锁
2018-02-06 12:13:20.947  INFO 11719 --- [      Thread-28] com.sn.RedisLock.RedisDistributionLock   : Thread-28开始执行加锁
2018-02-06 12:13:20.948  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30开始执行加锁
2018-02-06 12:13:20.948  INFO 11719 --- [      Thread-33] com.sn.RedisLock.RedisDistributionLock   : Thread-33开始执行加锁
2018-02-06 12:13:21.033  INFO 11719 --- [      Thread-16] com.sn.RedisLock.RedisDistributionLock   : Thread-16加锁成功++++++++111111111
2018-02-06 12:13:21.037  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24等待加锁,睡眠100毫秒
2018-02-06 12:13:21.037  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22等待加锁,睡眠100毫秒
2018-02-06 12:13:21.037  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.037  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27等待加锁,睡眠100毫秒
2018-02-06 12:13:21.037  INFO 11719 --- [      Thread-28] com.sn.RedisLock.RedisDistributionLock   : Thread-28等待加锁,睡眠100毫秒
2018-02-06 12:13:21.037  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20等待加锁,睡眠100毫秒
2018-02-06 12:13:21.038  INFO 11719 --- [      Thread-26] com.sn.RedisLock.RedisDistributionLock   : Thread-26等待加锁,睡眠100毫秒
2018-02-06 12:13:21.038  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23等待加锁,睡眠100毫秒
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30等待加锁,睡眠100毫秒
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34等待加锁,睡眠100毫秒
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-33] com.sn.RedisLock.RedisDistributionLock   : Thread-33等待加锁,睡眠100毫秒
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
Thread-16任务执行中
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-16] com.sn.RedisLock.RedisDistributionLock   : Thread-16执行解锁==========
2018-02-06 12:13:21.039  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.040  INFO 11719 --- [      Thread-18] com.sn.RedisLock.RedisDistributionLock   : Thread-18等待加锁,睡眠100毫秒
2018-02-06 12:13:21.040  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17等待加锁,睡眠100毫秒
2018-02-06 12:13:21.040  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.040  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.041  INFO 11719 --- [      Thread-16] com.sn.RedisLock.RedisDistributionLock   : Thread-16解锁成功-----------------
2018-02-06 12:13:21.145  INFO 11719 --- [      Thread-28] com.sn.RedisLock.RedisDistributionLock   : Thread-28加锁成功++++++++111111111
2018-02-06 12:13:21.146  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22等待加锁,睡眠100毫秒
2018-02-06 12:13:21.147  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24等待加锁,睡眠100毫秒
2018-02-06 12:13:21.147  INFO 11719 --- [      Thread-33] com.sn.RedisLock.RedisDistributionLock   : Thread-33等待加锁,睡眠100毫秒
Thread-28任务执行中
2018-02-06 12:13:21.147  INFO 11719 --- [      Thread-28] com.sn.RedisLock.RedisDistributionLock   : Thread-28执行解锁==========
2018-02-06 12:13:21.148  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.148  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34等待加锁,睡眠100毫秒
2018-02-06 12:13:21.148  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20等待加锁,睡眠100毫秒
2018-02-06 12:13:21.149  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.149  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.149  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.149  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30等待加锁,睡眠100毫秒
2018-02-06 12:13:21.149  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.149  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.150  INFO 11719 --- [      Thread-28] com.sn.RedisLock.RedisDistributionLock   : Thread-28解锁成功-----------------
2018-02-06 12:13:21.150  INFO 11719 --- [      Thread-26] com.sn.RedisLock.RedisDistributionLock   : Thread-26加锁成功++++++++111111111
Thread-26任务执行中
2018-02-06 12:13:21.151  INFO 11719 --- [      Thread-26] com.sn.RedisLock.RedisDistributionLock   : Thread-26执行解锁==========
2018-02-06 12:13:21.151  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27等待加锁,睡眠100毫秒
2018-02-06 12:13:21.151  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23等待加锁,睡眠100毫秒
2018-02-06 12:13:21.151  INFO 11719 --- [      Thread-18] com.sn.RedisLock.RedisDistributionLock   : Thread-18等待加锁,睡眠100毫秒
2018-02-06 12:13:21.151  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.151  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17等待加锁,睡眠100毫秒
2018-02-06 12:13:21.152  INFO 11719 --- [      Thread-26] com.sn.RedisLock.RedisDistributionLock   : Thread-26解锁成功-----------------
2018-02-06 12:13:21.251  INFO 11719 --- [      Thread-33] com.sn.RedisLock.RedisDistributionLock   : Thread-33加锁成功++++++++111111111
2018-02-06 12:13:21.253  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22等待加锁,睡眠100毫秒
Thread-33任务执行中
2018-02-06 12:13:21.254  INFO 11719 --- [      Thread-33] com.sn.RedisLock.RedisDistributionLock   : Thread-33执行解锁==========
2018-02-06 12:13:21.254  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20等待加锁,睡眠100毫秒
2018-02-06 12:13:21.254  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24等待加锁,睡眠100毫秒
2018-02-06 12:13:21.254  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23等待加锁,睡眠100毫秒
2018-02-06 12:13:21.254  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-33] com.sn.RedisLock.RedisDistributionLock   : Thread-33解锁成功-----------------
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.255  INFO 11719 --- [      Thread-18] com.sn.RedisLock.RedisDistributionLock   : Thread-18加锁成功++++++++111111111
2018-02-06 12:13:21.256  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27等待加锁,睡眠100毫秒
Thread-18任务执行中
2018-02-06 12:13:21.256  INFO 11719 --- [      Thread-18] com.sn.RedisLock.RedisDistributionLock   : Thread-18执行解锁==========
2018-02-06 12:13:21.257  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17等待加锁,睡眠100毫秒
2018-02-06 12:13:21.257  INFO 11719 --- [      Thread-18] com.sn.RedisLock.RedisDistributionLock   : Thread-18解锁成功-----------------
2018-02-06 12:13:21.359  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20加锁成功++++++++111111111
2018-02-06 12:13:21.360  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23等待加锁,睡眠100毫秒
2018-02-06 12:13:21.360  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24等待加锁,睡眠100毫秒
2018-02-06 12:13:21.360  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22等待加锁,睡眠100毫秒
2018-02-06 12:13:21.360  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
Thread-20任务执行中
2018-02-06 12:13:21.361  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.361  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20执行解锁==========
2018-02-06 12:13:21.361  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.362  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.362  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30等待加锁,睡眠100毫秒
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-20] com.sn.RedisLock.RedisDistributionLock   : Thread-20解锁成功-----------------
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17等待加锁,睡眠100毫秒
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27等待加锁,睡眠100毫秒
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34等待加锁,睡眠100毫秒
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.363  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.465  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22加锁成功++++++++111111111
Thread-22任务执行中
2018-02-06 12:13:21.466  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22执行解锁==========
2018-02-06 12:13:21.466  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24等待加锁,睡眠100毫秒
2018-02-06 12:13:21.466  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-22] com.sn.RedisLock.RedisDistributionLock   : Thread-22解锁成功-----------------
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17等待加锁,睡眠100毫秒
2018-02-06 12:13:21.468  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.469  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.469  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34加锁成功++++++++111111111
Thread-34任务执行中
2018-02-06 12:13:21.469  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34执行解锁==========
2018-02-06 12:13:21.470  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.470  INFO 11719 --- [      Thread-34] com.sn.RedisLock.RedisDistributionLock   : Thread-34解锁成功-----------------
2018-02-06 12:13:21.470  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.570  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23加锁成功++++++++111111111
Thread-23任务执行中
2018-02-06 12:13:21.571  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23执行解锁==========
2018-02-06 12:13:21.571  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24等待加锁,睡眠100毫秒
2018-02-06 12:13:21.572  INFO 11719 --- [      Thread-23] com.sn.RedisLock.RedisDistributionLock   : Thread-23解锁成功-----------------
2018-02-06 12:13:21.573  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27加锁成功++++++++111111111
Thread-27任务执行中
2018-02-06 12:13:21.574  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27执行解锁==========
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-27] com.sn.RedisLock.RedisDistributionLock   : Thread-27解锁成功-----------------
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30等待加锁,睡眠100毫秒
2018-02-06 12:13:21.575  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.576  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.676  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24加锁成功++++++++111111111
Thread-24任务执行中
2018-02-06 12:13:21.676  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24执行解锁==========
2018-02-06 12:13:21.677  INFO 11719 --- [      Thread-24] com.sn.RedisLock.RedisDistributionLock   : Thread-24解锁成功-----------------
2018-02-06 12:13:21.677  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17加锁成功++++++++111111111
Thread-17任务执行中
2018-02-06 12:13:21.678  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17执行解锁==========
2018-02-06 12:13:21.678  INFO 11719 --- [      Thread-17] com.sn.RedisLock.RedisDistributionLock   : Thread-17解锁成功-----------------
2018-02-06 12:13:21.681  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30加锁成功++++++++111111111
Thread-30任务执行中
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30执行解锁==========
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21等待加锁,睡眠100毫秒
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.682  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.683  INFO 11719 --- [      Thread-30] com.sn.RedisLock.RedisDistributionLock   : Thread-30解锁成功-----------------
2018-02-06 12:13:21.788  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21加锁成功++++++++111111111
Thread-21任务执行中
2018-02-06 12:13:21.789  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21执行解锁==========
2018-02-06 12:13:21.789  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.789  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.790  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.790  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31等待加锁,睡眠100毫秒
2018-02-06 12:13:21.790  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.790  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.790  INFO 11719 --- [      Thread-21] com.sn.RedisLock.RedisDistributionLock   : Thread-21解锁成功-----------------
2018-02-06 12:13:21.895  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31加锁成功++++++++111111111
Thread-31任务执行中
2018-02-06 12:13:21.896  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31执行解锁==========
2018-02-06 12:13:21.896  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:21.896  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:21.896  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:21.896  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25等待加锁,睡眠100毫秒
2018-02-06 12:13:21.896  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:21.897  INFO 11719 --- [      Thread-31] com.sn.RedisLock.RedisDistributionLock   : Thread-31解锁成功-----------------
2018-02-06 12:13:22.002  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25加锁成功++++++++111111111
Thread-25任务执行中
2018-02-06 12:13:22.002  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25执行解锁==========
2018-02-06 12:13:22.003  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15等待加锁,睡眠100毫秒
2018-02-06 12:13:22.003  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:22.003  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:22.003  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:22.003  INFO 11719 --- [      Thread-25] com.sn.RedisLock.RedisDistributionLock   : Thread-25解锁成功-----------------
2018-02-06 12:13:22.107  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15加锁成功++++++++111111111
Thread-15任务执行中
2018-02-06 12:13:22.108  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15执行解锁==========
2018-02-06 12:13:22.108  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:22.108  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:22.108  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19等待加锁,睡眠100毫秒
2018-02-06 12:13:22.109  INFO 11719 --- [      Thread-15] com.sn.RedisLock.RedisDistributionLock   : Thread-15解锁成功-----------------
2018-02-06 12:13:22.212  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19加锁成功++++++++111111111
Thread-19任务执行中
2018-02-06 12:13:22.212  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19执行解锁==========
2018-02-06 12:13:22.213  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:22.213  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29等待加锁,睡眠100毫秒
2018-02-06 12:13:22.213  INFO 11719 --- [      Thread-19] com.sn.RedisLock.RedisDistributionLock   : Thread-19解锁成功-----------------
2018-02-06 12:13:22.316  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29加锁成功++++++++111111111
Thread-29任务执行中
2018-02-06 12:13:22.317  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29执行解锁==========
2018-02-06 12:13:22.318  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32等待加锁,睡眠100毫秒
2018-02-06 12:13:22.318  INFO 11719 --- [      Thread-29] com.sn.RedisLock.RedisDistributionLock   : Thread-29解锁成功-----------------
2018-02-06 12:13:22.421  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32加锁成功++++++++111111111
Thread-32任务执行中
2018-02-06 12:13:22.421  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32执行解锁==========
2018-02-06 12:13:22.422  INFO 11719 --- [      Thread-32] com.sn.RedisLock.RedisDistributionLock   : Thread-32解锁成功-----------------

参考文章:

http://blog.csdn.net/fengshizty/article/details/53561562

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,133评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,682评论 3 390
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,784评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,508评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,603评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,607评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,604评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,359评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,805评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,121评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,280评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,959评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,588评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,206评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,442评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,193评论 2 367
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,144评论 2 352

推荐阅读更多精彩内容

  • 分布式锁的解决方式 基于数据库表做乐观锁,用于分布式锁。(适用于小并发) 使用memcached的add()方法,...
    xiaolyuh阅读 15,514评论 6 17
  • SETNX命令简介 命令格式 SETNX key value 将 key 的值设为 value,当且仅当 key ...
    tangstream阅读 1,961评论 0 2
  • 很难入睡,今天经历很多,以为想要放过自己放过你,选择接受相亲,可是真不是想的那么简单,接受不了其他所有人。大哭一场...
    沈潇潇阅读 186评论 0 0
  • 落下帷幕的疲惫 颗颗 落入玉盘 散落出的悲伤 苍白无力 周末的夜 憨实的无忧 点点星光 把心引上月亮之上 不羁的寒...
    流沙的岁月阅读 380评论 0 10
  • 最近明星出轨的新闻长江后浪推前浪,先有文章,林丹,到最近的陈思城,贵圈的出轨现象仿佛已经成了常态! ...
    夜未秧歌阅读 372评论 4 3