java redis分布式锁_java 基于redis分布式锁

packagecom.example.demo;importorg.apache.commons.lang3.StringUtils;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.StringRedisTemplate;importorg.springframework.stereotype.Component;importjava.util.concurrent.TimeUnit;

@Componentpublic classDistributedLockComponent {

@Autowired

StringRedisTemplate stringRedisTemplate;private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());/***

* @desc 加锁

*@paramkey

*@paramvalue

*@paramtimeout 超时时间

*@paramautoReleaseTime 自动释放锁时间*/

public boolean lock(String key, String value, long timeout, longautoReleaseTime) {boolean flag = true;long time =System.currentTimeMillis();long maxTime = time +timeout;//自旋等待-如果在指定时间内还没获取到锁就退出自旋,并且设置过期时间避免死锁。

while (!stringRedisTemplate.opsForValue().setIfAbsent(key, value) && time <=maxTime) {try{

TimeUnit.MICROSECONDS.sleep(10);

}catch(InterruptedException e) {

Thread.currentThread().interrupt();

flag= false;break;

}

time=System.currentTimeMillis();

}//设置过期时间

if(flag) {

stringRedisTemplate.expire(key, autoReleaseTime, TimeUnit.MILLISECONDS);

}returnflag;

}/***

* @desc 解锁

*@paramkey

*@paramvalue*/

public voidunLock(String key, String value) {try{if(StringUtils.isNotBlank(stringRedisTemplate.opsForValue().get(key))&&stringRedisTemplate.opsForValue().get(key).equals(value)) {

stringRedisTemplate.delete(key);

}

}catch(Exception e) {

logger.error(e.getMessage(), e);

}

}/*** 扣减库存*/

publicString decreaseStock(String key, String value){try{

lock(key,value,6000,6000 * 2);

}catch(Exception e){

logger.error(e.getMessage(),e);

}finally{

unLock(key,value);

}return "";

}/*** 测试可模拟多个线程扣减库存

*@paramskuId 商品ID*/

publicString test(String skuId) {

decreaseStock("KEY_SKU_"+skuId, skuId);//线程1

new Thread(()->{

decreaseStock("KEY_SKU_"+skuId, skuId);

});//线程2

new Thread(()->{

decreaseStock("KEY_SKU_"+skuId, skuId);

});return "";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值