redis实现秒杀demo

代码

package com.prosay.redis;

import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class SaleRunable implements Runnable{
    String productKey="iphone8";//监视的key 当前秒杀商品的数量  
    Jedis jedis = new Jedis("localhost");
    String userName;
    public SaleRunable(String userName){
        this.userName = userName;
    }

    @Override
    public void run(){
        //商品的key  , 秒杀有个数量
        //watch 监视一个key,当事务执行之前这个key发生了改变,事务会被打断
        jedis.watch(productKey);
        String value = jedis.get(productKey);
        int num = Integer.valueOf(value);
        //这次秒杀的商品是100个iphone8 
        if(num<=100&&num>=1){
            //开启事务
            Transaction tx = jedis.multi();
            //减少一个商品数量
            tx.incrBy(productKey, -1);
            //提交事务,如果商品数量发生了改动 则会返回null
            List<Object> list = tx.exec();
            if(list==null || list.size()==0){
                System.out.println(userName+"商品抢购失败!");
            }else{
                for(Object success : list){
                    System.out.println(userName+"("+success.toString()+")商品抢购成功,当前抢购成功的人数是:"+(1-(num-100)));
                }
            }
        }else{
            System.out.println(userName+"商品已经被抢完了");
        }
        jedis.close();
    }

}

测试

package com.prosay.redis;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import redis.clients.jedis.Jedis;

public class TestMs {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        jedis.set("iphone8","100");
        jedis.close();
        //玩多线程
        ExecutorService executor = Executors.newFixedThreadPool(20);

        for(int i = 0 ; i < 1000; i++){
            executor.execute(new SaleRunable("user"+i));
        }
        executor.shutdown();

    }

}

转自:https://blog.csdn.net/lzh657083979/article/details/77917088

猜你喜欢

转载自www.cnblogs.com/azhqiang/p/9145830.html