redis实现分布式锁实现步骤


import java.util.concurrent.locks.Lock;

/**
 * @author lsy  分布式锁
 * @version 1.0
 * @date 2022/7/13 11:04
 */
public class Tests {




    public static void main(String[] args){
      Thread th1=new Thread(new MyThread(),"线程1");
      Thread th2=new Thread(new MyThread(),"线程2");
        th1.start();
        th2.start();

    }


    static class  MyThread implements Runnable{
        Lock lock=new RedisLock();
        @Override
        public void run() {
            new Orders().orders();
            lock.lock();
            new Stocks().getsocks();
            lock.unlock();
        }
    }

}

/**
 * @author lsy
 * @version 1.0
 * @date 2022/7/13 11:00
 */
public class Stocks {

    private static int KETYNUMBER=1;


    public boolean getsocks(){
        if(KETYNUMBER>0){
            KETYNUMBER--;
            System.out.println("---------------------------------------");
            System.out.println(Thread.currentThread().getName()+"减库成功");
            return true;
        }else{
            System.out.println(Thread.currentThread().getName()+"减库失败");
            return false;
        }
    }
}

/**
 * @author lsy
 * @version 1.0
 * @date 2022/7/13 10:58
 */
public class Orders {


    public void orders(){
        //Thread 获取线程的名称
        System.out.println(Thread.currentThread().getName()+"下单成功!");

    }
}


import redis.clients.jedis.Jedis;

import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public  class RedisLock implements Lock {

    ThreadLocal<Jedis> jedis = new ThreadLocal<Jedis>();
    private static String LOCK_NAME = "LOCK";
    private static String REQUEST_ID = "111111";



    public void lock() {
        if (jedis.get() ==null){
            Jedis jedis = new Jedis("ip", 6379);
            jedis.auth("密码");
            this.jedis.set(jedis);
        }

        if (tryLock())
            jedis.get().set(LOCK_NAME,REQUEST_ID,"NX","PX",5000);
    }

    public boolean tryLock() {
        System.out.println("");
        while(true){
            Long ret = jedis.get().setnx(LOCK_NAME,REQUEST_ID);
            if (ret==1)
                return true;
        }
    }

    public void unlock() {
        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
        Object eval = jedis.get().eval(script, Collections.singletonList(LOCK_NAME), Collections.singletonList(REQUEST_ID));
    }

    public Condition newCondition() {
        return null;
    }

    public void lockInterruptibly() throws InterruptedException {

    }

    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }

}
<dependency>
                  <groupId>redis.clients</groupId>
                  <artifactId>jedis</artifactId>
                  <version>2.6.0</version>
                  <type>jar</type>
                  <scope>compile</scope>
              </dependency>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值