2024年最新《Redis实战篇》三、优惠券秒杀(2)

文章介绍了如何使用Redis和Java实现全局唯一的ID生成,以及在秒杀优惠券系统中处理并发问题,包括使用CountDownLatch同步多线程和应用乐观锁策略避免库存超卖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间戳:31bit,以秒为单位,可以使用69年

序列号:32bit,秒内的计数器,支持每秒产生2^32个不同ID

3.2 Redis实现全局唯一Id

/\*\*
 \* @author lxy
 \* @version 1.0
 \* @Description ID生成器
 \* @date 2022/12/12 12:48
 \*/
@Component
public class RedisIdWorker {

    /\*\*
 \* 开始时间戳
 \*/
    private static final long BEGIN\_TIMESTAMP = 1670803200L;

    /\*\*
 \* 序列号的位数
 \*/
    private static final long COUNT\_BITS = 32;

    private StringRedisTemplate stringRedisTemplate;

    public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public long nextId(String keyPrefix){
        // 1.生成时间戳
        LocalDateTime now = LocalDateTime.now();
        long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
        long timestamp = nowSecond - BEGIN\_TIMESTAMP;

        // 2.生成序列号
        //2.1 获取当天日期,精确到天
        String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        // 2.2自增长
        Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);

        // 3.拼接并返回
        return timestamp << COUNT\_BITS | count;
    }

}

测试类

@Test
public void testIdWorker() throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(300);

    Runnable task = ()->{
        for (int i = 0; i < 100; i++) {
            long id = redisIdWorker.nextId("order");
            System.out.println("id = "+id);
        }
        countDownLatch.countDown();
    };
    long begin = System.currentTimeMillis();
    for (int i = 0; i < 300; i++) {
        es.submit(task);
    }
    countDownLatch.await();
    long end = System.currentTimeMillis();
    System.out.println("time = "+(end - begin));
}

知识小贴士:关于countdownlatch

countdownlatch名为信号枪:主要的作用是同步协调在多线程的等待于唤醒问题

我们如果没有CountDownLatch ,那么由于程序是异步的,当异步程序没有执行完时,主线程就已经执行完了,然后我们期望的是分线程全部走完之后,主线程再走,所以我们此时需要使用到CountDownLatch

CountDownLatch 中有两个最重要的方法

1、countDown

2、await

await 方法 是阻塞方法,我们担心分线程没有执行完时,main线程就先执行,所以使用await可以让main线程阻塞,那么什么时候main线程不再阻塞呢?当CountDow

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值