Giter Club home page Giter Club logo

distributedlock's People

Contributors

winflex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

distributedlock's Issues

这里有个问题

		if (isTimeExpired(currLockInfo.getExpires())) {
			// 锁超时了
			LockInfo oldLockInfo = LockInfo.fromString(jedis.getSet(lockKey, newLockInfoJson));
                            //,这里需要判断redis所内容是否为当前锁,要不然并发情况会被多个线程获得锁
                            //String newSetLockInfoJson = redisTemplate.opsForValue().get(lockKey);
			//if (oldLockInfo != null && isTimeExpired(oldLockInfo.getExpires()) && 
                            //newSetLockInfoJson.equals(newLockInfoJson)) {
			if (oldLockInfo != null && isTimeExpired(oldLockInfo.getExpires())) {
				// 成功获取到锁, 设置相关标识
				logger.debug("{} get lock(new), lockInfo: {}", Thread.currentThread().getName(), newLockInfoJson);
				locked = true;
				return true;
			}
		}

运行过程中异常中断,锁超时时使用getset会出现争抢

if (isTimeExpired(currLockInfo.getExpires()))
运行过程中异常中断,redis中会有一条锁记录,当程序重新启动后,多个线程会争抢锁,导致锁异常。

LockInfo currLockInfo = LockInfo.fromString(currLockInfoJson);
			// 竞争条件只可能出现在锁超时的情况, 因为如果没有超时, 线程发现锁并不是被自己持有, 线程就不会去动value
			if (isTimeExpired(currLockInfo.getExpires())) {
				// 锁超时了
				LockInfo oldLockInfo = LockInfo.fromString(jedis.getSet(lockKey, newLockInfoJson));
				if (oldLockInfo != null && isTimeExpired(oldLockInfo.getExpires())) {
					// 成功获取到锁, 设置相关标识
					logger.debug("{} get lock(new), lockInfo: {}", Thread.currentThread().getName(), newLockInfoJson);
					locked = true;
					return true;
				}
			} else {
				// 锁未超时, 不会有竞争情况
				if (isHeldByCurrentThread(currLockInfo)) { // 当前线程持有
					// 成功获取到锁, 设置相关标识
					currLockInfo.setExpires(serverTimeMillis() + lockExpires + 1); // 设置新的锁超时时间
					currLockInfo.incCount();
					jedis.set(lockKey, currLockInfo.toString());
					logger.debug("{} get lock(inc), lockInfo: {}", Thread.currentThread().getName(), currLockInfo);
					locked = true;
					return true;
				}
			}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.