在集群等多服务器(意味着多个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集成时的依赖
org.springframework.boot
spring-boot-starter-data-redis
1.5.4.RELEASE
使用Spring MVC(带有Spring Session Redis)时的依赖
org.springframework.session
spring-session-data-redis
1.3.1.RELEASE
使用Spring MVC(不带有Spring Session Redis)时的依赖
org.springframework.data
spring-data-redis
1.0.2.RELEASE
redis.clients
jedis
2.9.0
代码实现
分布式锁接口
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 redisTemplate;
RedisDistributionLock(RedisTemplate redisTemplate){
this.redisTemplate = redisTemplate;
}
// @SuppressWarnings("unchecked")
// private static RedisTemplate redisTemplate = (RedisTemplate) 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() {
@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
模拟测试
测试代码
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