mirror of
https://gitee.com/52itstyle/spring-boot-seckill.git
synced 2026-05-13 19:02:39 +00:00
预估秒杀人数,设定自动释放锁时间
This commit is contained in:
@@ -26,8 +26,8 @@ public class SeckillDistributedServiceImpl implements ISeckillDistributedService
|
||||
public Result startSeckilRedisLock(long seckillId,long userId) {
|
||||
boolean res=false;
|
||||
try {
|
||||
//尝试获取锁,最多等待10秒,上锁以后10秒自动解锁(实际项目中推荐这种,以防出现死锁)
|
||||
res = RedissLockUtil.tryLock(seckillId+"", TimeUnit.SECONDS, 20, 10);
|
||||
//尝试获取锁,最多等待3秒,上锁以后20秒自动解锁(实际项目中推荐这种,以防出现死锁)、这里根据预估秒杀人数,设定自动释放锁时间
|
||||
res = RedissLockUtil.tryLock(seckillId+"", TimeUnit.SECONDS, 3, 20);
|
||||
String nativeSql = "SELECT number FROM seckill WHERE seckill_id=?";
|
||||
Object object = dynamicQuery.nativeQueryObject(nativeSql, new Object[]{seckillId});
|
||||
Long number = ((Number) object).longValue();
|
||||
|
||||
@@ -56,7 +56,7 @@ public class SeckillDistributedController {
|
||||
executor.execute(task);
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
Thread.sleep(15000);
|
||||
Long seckillCount = seckillService.getSeckillCount(seckillId);
|
||||
LOGGER.info("一共秒杀出{}件商品",seckillCount);
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
Reference in New Issue
Block a user