一、前言
前二章我们讲解的redis事务和redis乐观锁,您可以点击下面文章查看。
本章我们继续在上面的基础上用乐观锁实现秒杀。
二、秒杀的实现
我们先来设置一个场景,假设有50个商品,1000个人抢购。那么最终会有50个人买到商品。
在实现上我们配合着线程池来实现。具体代码如下:
public class Main {
public static void main(String[] args) {
RedisConnect.redisConn();
String redisKey = "second";
ExecutorService executorService = Executors.newFixedThreadPool(20);
try {
Jedis jedis = new Jedis("127.0.0.1");
// 初始值
jedis.set(redisKey, "0");
jedis.close();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < 1000; i++) {
executorService.execute(() -> {
Jedis jedis1 = new Jedis("127.0.0.1");
try {
jedis1.watch(redisKey);
String redisValue = jedis1.get(redisKey);
int valInteger = Integer.valueOf(redisValue);
String userInfo = UUID.randomUUID().toString();
// 没有秒完
if (valInteger < 20) {
Transaction tx = jedis1.multi();
tx.incr(redisKey);
List list = tx.exec();
// 秒成功 失败返回空list而不是空
if (list != null && list.size() > 0) {
System.out.println("用户:" + userInfo + ",秒杀成功! 当前成功人数:" + (valInteger + 1));
}
// 版本变化,被别人抢了。
else {
System.out.println("用户:" + userInfo + ",秒杀失败");
}
}
// 秒完了
else {
System.out.println("已经有20人秒杀成功,秒杀结束");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis1.close();
}
});
}
executorService.shutdown();
}
}
执行结果如下:
代码中用线程池能防止内存资源瞬间爆棚。这块代码也好理解,这里我就不过多介绍了。
三、结尾
好了,本节就讲到这里,希望对大家有所帮助。
另外大家帮忙关注一下我的微信公众号,关注后有大量课程想赠。