首页 > 编程语言 > 详细

java 基于redis分布式锁

时间:2020-08-07 09:48:52      阅读:81      评论:0      收藏:0      [点我收藏+]

1、基于redis分布式锁

package com.example.demo;



import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class DistributedLockComponent {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     *
     * @desc 加锁
     * @param key
     * @param value
     * @param timeout 超时时间
     * @param autoReleaseTime 自动释放锁时间
     */
    public boolean lock(String key, String value, long timeout, long autoReleaseTime) {
        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);
        }

        return flag;
    }

    /**
     *
     * @desc 解锁
     * @param key
     * @param value
     */
    public void unLock(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);
        }
    }


    /**
     * 扣减库存
     */
    public String 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 "";
    }


    /**
     *  测试可模拟多个线程扣减库存
     * @param skuId 商品ID
     */
    public String 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 "";
    }


}

 

java 基于redis分布式锁

原文:https://www.cnblogs.com/chenweichu/p/13450013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!