需要导入的依赖
/*
需要的依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
* redission 实现方法
* */
RLock redissionLock=redisson.getLock("lockkey");
// 如果其他线程来了,会阻塞等待
try {
redissionLock.lock();//jedis.setnx 并且设置了过期时间,每隔十秒会检测一下防止服务器宕机
int stock= (int) redisUtil.get("key");
if(stock>0){
redisUtil.set("key",--stock);
System.out.println("stock = " + stock+" ----->加锁成功");
}
else{
System.out.println("stock = " + stock+" ----->加锁失败");
}
}
finally {
redissionLock.unlock();
}