1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class GrabMysqlLockServiceImpl implements GrabService{ @Autowired private MysqlLock lock; @Autowired OrderService orderService; ThreadLocal<TblOrderLock> orderLock = new ThreadLocal<>(); @Override public String grabOrder(int orderId, int driverId) { TblOrderLock ol = new TblOrderLock(); ol.setOrderId(orderId); ol.setDriverId(driverId);
orderLock.set(ol); lock.setOrderLockThreadLocal(orderLock);
lock.lock(); try{ System.out.println("司机:"+driverId+" 执行抢单逻辑"); boolean b = orderService.grab(orderId, driverId); if(b) { System.out.println("司机:"+driverId+" 抢单成功"); }else{ System.out.println("司机:"+driverId+" 抢单失败"); } }finally{ lock.unlock(); } return null; } }
|