商品秒杀 使用redis乐观锁和事务,用多线程测试

来源:https://www.jianshu.com/p/93cd65d07b56
public class RedisTest1Sub {
private static String key = “macbook”;

private static String num = "100";

private static ExecutorService executorService = Executors.newFixedThreadPool(8);

public static void main(String[] args) {
try{
for (int i = 1; i <= 600; i++) {
executorService.submit(new RedisTest1(“顾客”+i,key));

        }
    }finally {
        executorService.shutdown();
    }
}

}

public class RedisTest1 implements Runnable {
private Jedis jedis = new Jedis(“localhost”, 6379);
private String customerName;

private String key;

public RedisTest1(String customerName, String key) {
    this.customerName = customerName;
    this.key = key;
}


@Override
public void run() {
    boolean success = false;
    String data;
    int currentNum;
    while (!success) {//可重复抢购直到成功
        //通过watch实现redis的incr(原子递增操作)
        jedis.watch(key);
        data = jedis.get(key);
        currentNum = Integer.parseInt(data);
        if (currentNum > 0) {
            //开启事务
            Transaction transaction = jedis.multi();
            //设置新值,如果key的值被其它连接的客户端修改,那么当前连接的exec命令将执行失败
            currentNum--;
            transaction.set(key, String.valueOf(currentNum));
            List res = transaction.exec();
            if (res==null || res.size() == 0) {
                System.out.println(res);
                System.out.println(customerName + " 抢购失败");
                success = false;
            } else {
                System.out.println(res);
                success = true;
                System.out.println(customerName + " 抢购成功,[" + key + "]剩余:" + currentNum);
            }
        } else {
            System.out.println("商品售空,活动结束!");
            System.exit(0);
        }
    }
}

}

在生产环境直接使用
public class RedisTes {
@Autowired
StringRedisTemplate stringRedisTemplate;

//实现秒杀
public void miaosha(){
    String key = "goods";

    String data ="";
    int currentNum ;
        //进行watch监听
    stringRedisTemplate.watch(key);
    data = stringRedisTemplate.opsForValue().get(key);
    currentNum = Integer.parseInt(data);
        if(currentNum>0){
            //事务
            stringRedisTemplate.setEnableTransactionSupport(true);
            stringRedisTemplate.multi();
            currentNum--;
            stringRedisTemplate.opsForValue().set(key,String.valueOf(currentNum));
            List<Object> exec = stringRedisTemplate.exec();
            if(exec==null || exec.size()==0){
                System.out.println(Thread.currentThread().getName()+"抢购失败");

            }else {
                System.out.println(Thread.currentThread().getName()+"抢购成功");

            }
        }else{
            System.out.println(Thread.currentThread().getName()+"商品售罄");
        }

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值