实战篇9
设立一个在jvm外的锁监视器,可以处理多线程的问题
实战篇10
获取锁的时候,要同时发生获取锁以及设置到期时间。
实战篇11
thinking:JAVA中的自动拆箱与装箱?
【Java基础】自动拆装箱_Elephant_King的博客-CSDN博客
TRUE.equals():保证不会有空指针异常。
package com.hmdp.utils;public interface ILock {/*** 尝试获取锁* @param timeoutSec 锁持有的超时时间,过期后自动释放* @return true代表获取锁成功;false代表获取锁失败*/boolean tryLock(long timeoutSec);/*** 释放锁*/void unlock();
}
package com.hmdp.utils;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
public class SimpleRedisLock implements ILock {private String name;private StringRedisTemplate stringRedisTemplate;private static final String KEY_PREFIX = "lock:";public SimpleRedisLock(String name, StringRedisTemplate stringRedisTemplate) {this.name = name;this.stringRedisTemplate = stringRedisTemplate;}@Overridepublic boolean tryLock(long timeoutSec) {//获取线程标示long threadId = Thread.currentThread().getId();//释放锁Boolean success = stringRedisTemplate.opsForValue().setIfAbsent(KEY_PREFIX + name, threadId + "", timeoutSec, TimeUnit.SECONDS);return Boolean.TRUE.equals(success);}@Overridepublic void unlock() {//释放锁stringRedisTemplate.delete(KEY_PREFIX + name);}
}
package com.hmdp.service.impl;import com.hmdp.dto.Result;
import com.hmdp.entity.SeckillVoucher;
import com.hmdp.entity.User;
import com.hmdp.entity.Voucher;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.VoucherOrderMapper;
import com.hmdp.service.ISeckillVoucherService;
import com.hmdp.service.IVoucherOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisIdWorker;
import com.hmdp.utils.SimpleRedisLock;
import com.hmdp.utils.UserHolder;
import org.springframework.aop.framework.AopContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;
import java.time.LocalDateTime;/*** <p>* 服务实现类* </p>** @author 虎哥* @since 2021-12-22*/
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {@Resourceprivate ISeckillVoucherService seckillVoucherService;@Resourceprivate RedisIdWorker redisIdWorker;@Resourceprivate StringRedisTemplate stringRedisTemplate;@Overridepublic Result seckillVoucher(Long voucherId) {//1,查询优惠券SeckillVoucher voucher = seckillVoucherService.getById(voucherId);//2,判断秒杀是否开始if (voucher.getBeginTime().isAfter(LocalDateTime.now())) {//尚未开始return Result.fail("秒杀尚未开始!");}//3,判断秒杀是否已经结束if (voucher.getBeginTime().isBefore(LocalDateTime.now())) {//已经结束return Result.fail("秒杀已经结束!");}//4,判断库存是否充足if (voucher.getStock() < 1) {//库存不足return Result.fail("库存不足!");}//一人一锁,提高效率//intern 保证指定的userId对应指定的锁//先获取锁,再完成以下方法,先完成方法,再释放锁,才能确保线程安全Long userId = UserHolder.getUser().getId();//synchronized(userId.toString().intern()){//获取代理对象(事务)//创建锁对象SimpleRedisLock lock = new SimpleRedisLock("order" + userId, stringRedisTemplate);//获取锁boolean isLock = lock.tryLock(1200);//判断是否获取锁成功if(!isLock){//获取锁失败,返回错误或重试return Result.fail("不允许重复下单");}try {//获取代理对象(事务)IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();return proxy.createVoucherOrder(voucherId);} finally {//释放锁lock.unlock();}// }}//加上事务,因为这里有两张表@Transactionalpublic Result createVoucherOrder(Long voucherId) {//5,一人一单Long userId = UserHolder.getUser().getId();//5.1 查询订单int count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();//5.2 判断是否存在if (count > 0) {// 用户已经购买过了return Result.fail("用户已经购买过一次!");}//6,扣减库存boolean success = seckillVoucherService.update().setSql("stock = stock - 1").eq("voucher_id", voucherId).gt("stock", 0).update();if (!success) {//扣除失败return Result.fail("库存不足!");}//7,创建订单VoucherOrder voucherOrder = new VoucherOrder();//7.1 订单idlong orderId = redisIdWorker.nextId("order");voucherOrder.setId(orderId);//7.2 用户idvoucherOrder.setUserId(userId);//7.3 优惠券idvoucherOrder.setVoucherId(voucherId);save(voucherOrder);//8,返回订单idreturn Result.ok(orderId);}}
thinking:我怎么知道什么时候要处理异常?
最全最详细的Java异常处理机制_两个系统对接 另一个系统有异常_我是波哩个波的博客-CSDN博客
实战篇12
设置锁的标识,避免误删别人的锁,以达到避免多线程并发的情况发生
实战篇13
thinking:hutool的UUID?
【工具类用法】Hutool里的生成唯一Id唯的工具类_hutool生成uuid-CSDN博客
thinking:java中long和string互转?
java中long和string互转_long转换string-CSDN博客
package com.hmdp.utils;
import cn.hutool.core.lang.UUID;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
public class SimpleRedisLock implements ILock {private String name;private StringRedisTemplate stringRedisTemplate;private static final String KEY_PREFIX = "lock:";private static final String ID_PREFIX = UUID.randomUUID().toString(true)+"-";public SimpleRedisLock(String name, StringRedisTemplate stringRedisTemplate) {this.name = name;this.stringRedisTemplate = stringRedisTemplate;}@Overridepublic boolean tryLock(long timeoutSec) {//获取线程标示String threadId = ID_PREFIX + Thread.currentThread().getId();//释放锁Boolean success = stringRedisTemplate.opsForValue().setIfAbsent(KEY_PREFIX + name, threadId, timeoutSec, TimeUnit.SECONDS);return Boolean.TRUE.equals(success);}@Overridepublic void unlock() {//获取线程标示String threadId = ID_PREFIX + Thread.currentThread().getId();//获取锁中的标示String id = stringRedisTemplate.opsForValue().get(KEY_PREFIX + name);//判断标示是否一致if(threadId.equals(id)){//释放锁stringRedisTemplate.delete(KEY_PREFIX + name);}}
}
实战篇14
如果仅仅用id 两个不同的jvm就会出现两个相同的id 也有可能出现bug。因此,要将这块原子性,因为当线程一因为回收垃圾产生阻塞情况,而锁因为超时而释放时,线程二就有机会趁机而入,从而获取锁,从而发生多线程并发的情况
实战篇15
lua的使用手册
Lua 教程 | 菜鸟教程
实战篇16
thinking:没有lua标志?
解决方案:下载其中一个就好,不要两个都下载,两个插件效果是一样的。两个都下载会导致idea卡死的哦。
LUA:
--比较线程标示与锁中的标示是否一致
if(redis.call('get',KEYS[1])== ARGV[1])then--释放锁 del keyreturn redis.call('del',KEYS[1])
end
return 0
JAVA:
package com.hmdp.utils;
import cn.hutool.core.lang.UUID;
import org.apache.ibatis.javassist.ClassPath;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
public class SimpleRedisLock implements ILock {private String name;private StringRedisTemplate stringRedisTemplate;private static final String KEY_PREFIX = "lock:";private static final String ID_PREFIX = UUID.randomUUID().toString(true)+"-";//提前读好这个文件,避免多次读取private static final DefaultRedisScript<Long> UNLOCK_SCRIPT;//因为是静态的,因此在静态代码块里面搞static {UNLOCK_SCRIPT = new DefaultRedisScript<>();UNLOCK_SCRIPT.setLocation(new ClassPathResource("unlock.lua"));UNLOCK_SCRIPT.setResultType(Long.class);}public SimpleRedisLock(String name, StringRedisTemplate stringRedisTemplate) {this.name = name;this.stringRedisTemplate = stringRedisTemplate;}@Overridepublic boolean tryLock(long timeoutSec) {//获取线程标示String threadId = ID_PREFIX + Thread.currentThread().getId();//释放锁Boolean success = stringRedisTemplate.opsForValue().setIfAbsent(KEY_PREFIX + name, threadId, timeoutSec, TimeUnit.SECONDS);return Boolean.TRUE.equals(success);}@Overridepublic void unlock() {//调用lua脚本stringRedisTemplate.execute(UNLOCK_SCRIPT, Collections.singletonList(KEY_PREFIX+name),ID_PREFIX + Thread.currentThread().getId());}
// @Override
// public void unlock() {
// //获取线程标示
// String threadId = ID_PREFIX + Thread.currentThread().getId();
// //获取锁中的标示
// String id = stringRedisTemplate.opsForValue().get(KEY_PREFIX + name);
// //判断标示是否一致
// if(threadId.equals(id)){
// //释放锁
// stringRedisTemplate.delete(KEY_PREFIX + name);
// }}
实战篇17
这个网站不知道为什么打不开。。
实战篇18
<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.13.6</version></dependency>
package com.hmdp.config;import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RedissonConfig {@Beanpublic RedissonClient redissonClient(){//配置Config config = new Config();config.useSingleServer().setAddress("redis://localhost:6379");//创建RedissonClient对象return Redisson.create(config);}
}
package com.hmdp.service.impl;import com.hmdp.dto.Result;
import com.hmdp.entity.SeckillVoucher;
import com.hmdp.entity.User;
import com.hmdp.entity.Voucher;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.VoucherOrderMapper;
import com.hmdp.service.ISeckillVoucherService;
import com.hmdp.service.IVoucherOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisIdWorker;
import com.hmdp.utils.SimpleRedisLock;
import com.hmdp.utils.UserHolder;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.aop.framework.AopContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;
import java.time.LocalDateTime;/*** <p>* 服务实现类* </p>** @author 虎哥* @since 2021-12-22*/
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {@Resourceprivate ISeckillVoucherService seckillVoucherService;@Resourceprivate RedisIdWorker redisIdWorker;@Resourceprivate StringRedisTemplate stringRedisTemplate;@Resourceprivate RedissonClient redissonClient;@Overridepublic Result seckillVoucher(Long voucherId) {//1,查询优惠券SeckillVoucher voucher = seckillVoucherService.getById(voucherId);//2,判断秒杀是否开始if (voucher.getBeginTime().isAfter(LocalDateTime.now())) {//尚未开始return Result.fail("秒杀尚未开始!");}//3,判断秒杀是否已经结束if (voucher.getBeginTime().isBefore(LocalDateTime.now())) {//已经结束return Result.fail("秒杀已经结束!");}//4,判断库存是否充足if (voucher.getStock() < 1) {//库存不足return Result.fail("库存不足!");}//一人一锁,提高效率//intern 保证指定的userId对应指定的锁//先获取锁,再完成以下方法,先完成方法,再释放锁,才能确保线程安全Long userId = UserHolder.getUser().getId();//synchronized(userId.toString().intern()){//获取代理对象(事务)//创建锁对象// SimpleRedisLock lock = new SimpleRedisLock("order:" + userId, stringRedisTemplate);RLock lock = redissonClient.getLock("lock:order:" + userId);//获取锁//不传参数,代表我失败了立即返回boolean isLock = lock.tryLock();//判断是否获取锁成功if(!isLock){//获取锁失败,返回错误或重试return Result.fail("不允许重复下单");}try {//获取代理对象(事务)IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();return proxy.createVoucherOrder(voucherId);} finally {//释放锁lock.unlock();}// }}//加上事务,因为这里有两张表@Transactionalpublic Result createVoucherOrder(Long voucherId) {//5,一人一单Long userId = UserHolder.getUser().getId();//5.1 查询订单int count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();//5.2 判断是否存在if (count > 0) {// 用户已经购买过了return Result.fail("用户已经购买过一次!");}//6,扣减库存boolean success = seckillVoucherService.update().setSql("stock = stock - 1").eq("voucher_id", voucherId).gt("stock", 0).update();if (!success) {//扣除失败return Result.fail("库存不足!");}//7,创建订单VoucherOrder voucherOrder = new VoucherOrder();//7.1 订单idlong orderId = redisIdWorker.nextId("order");voucherOrder.setId(orderId);//7.2 用户idvoucherOrder.setUserId(userId);//7.3 优惠券idvoucherOrder.setVoucherId(voucherId);save(voucherOrder);//8,返回订单idreturn Result.ok(orderId);}}