@Test void lock() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(10); int count = 200; CountDownLatch countDownLatch = new CountDownLatch(count); for (int i = 0; i < count/2; i++) { executorService.execute(() -> { if(zookeeperLockUtil.lock(10000L)) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } zookeeperLockUtil.releaseLock(10000L); log.debug("计数器当前值{}", countDownLatch.getCount()); countDownLatch.countDown(); } }); executorService.execute(() -> { if(zookeeperLockUtil.lock(10001L)) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } zookeeperLockUtil.releaseLock(10001L); log.debug("计数器当前值{}", countDownLatch.getCount()); countDownLatch.countDown(); } }); } countDownLatch.await(); executorService.shutdown(); Assert.assertTrue(countDownLatch.getCount() == 0); }
public boolean lock(long seckillId) { try { Map<Long, InterProcessMutex> map; String rootLockPath = "/goodskill"; Map<Long, InterProcessMutex> processMutexMap = threadLock.get(); if (processMutexMap.get(seckillId) == null) { processMutexMap.put(seckillId, new InterProcessMutex(client, rootLockPath + "/" + seckillId)); } boolean acquire = processMutexMap.get(seckillId).acquire(1000L, TimeUnit.MILLISECONDS); if (log.isDebugEnabled() && acquire) { log.debug("成功获取到zk锁,秒杀id{}", seckillId); } else { log.debug("未获取到zk锁,秒杀id{}", seckillId); } return acquire; } catch (Exception e) { log.warn("获取zk锁异常:{}", e.getMessage()); return false; } }
ZookeeperLockUtil { public boolean lock(long seckillId) { try { Map<Long, InterProcessMutex> map; String rootLockPath = "/goodskill"; Map<Long, InterProcessMutex> processMutexMap = threadLock.get(); if (processMutexMap.get(seckillId) == null) { processMutexMap.put(seckillId, new InterProcessMutex(client, rootLockPath + "/" + seckillId)); } boolean acquire = processMutexMap.get(seckillId).acquire(1000L, TimeUnit.MILLISECONDS); if (log.isDebugEnabled() && acquire) { log.debug("成功获取到zk锁,秒杀id{}", seckillId); } else { log.debug("未获取到zk锁,秒杀id{}", seckillId); } return acquire; } catch (Exception e) { log.warn("获取zk锁异常:{}", e.getMessage()); return false; } } }
ZookeeperLockUtil { public boolean lock(long seckillId) { try { Map<Long, InterProcessMutex> map; String rootLockPath = "/goodskill"; Map<Long, InterProcessMutex> processMutexMap = threadLock.get(); if (processMutexMap.get(seckillId) == null) { processMutexMap.put(seckillId, new InterProcessMutex(client, rootLockPath + "/" + seckillId)); } boolean acquire = processMutexMap.get(seckillId).acquire(1000L, TimeUnit.MILLISECONDS); if (log.isDebugEnabled() && acquire) { log.debug("成功获取到zk锁,秒杀id{}", seckillId); } else { log.debug("未获取到zk锁,秒杀id{}", seckillId); } return acquire; } catch (Exception e) { log.warn("获取zk锁异常:{}", e.getMessage()); return false; } } private  ZookeeperLockUtil(); }
ZookeeperLockUtil { public boolean lock(long seckillId) { try { Map<Long, InterProcessMutex> map; String rootLockPath = "/goodskill"; Map<Long, InterProcessMutex> processMutexMap = threadLock.get(); if (processMutexMap.get(seckillId) == null) { processMutexMap.put(seckillId, new InterProcessMutex(client, rootLockPath + "/" + seckillId)); } boolean acquire = processMutexMap.get(seckillId).acquire(1000L, TimeUnit.MILLISECONDS); if (log.isDebugEnabled() && acquire) { log.debug("成功获取到zk锁,秒杀id{}", seckillId); } else { log.debug("未获取到zk锁,秒杀id{}", seckillId); } return acquire; } catch (Exception e) { log.warn("获取zk锁异常:{}", e.getMessage()); return false; } } private  ZookeeperLockUtil(); boolean lock(long seckillId); boolean releaseLock(long seckillId); }
ZookeeperLockUtil { public boolean lock(long seckillId) { try { Map<Long, InterProcessMutex> map; String rootLockPath = "/goodskill"; Map<Long, InterProcessMutex> processMutexMap = threadLock.get(); if (processMutexMap.get(seckillId) == null) { processMutexMap.put(seckillId, new InterProcessMutex(client, rootLockPath + "/" + seckillId)); } boolean acquire = processMutexMap.get(seckillId).acquire(1000L, TimeUnit.MILLISECONDS); if (log.isDebugEnabled() && acquire) { log.debug("成功获取到zk锁,秒杀id{}", seckillId); } else { log.debug("未获取到zk锁,秒杀id{}", seckillId); } return acquire; } catch (Exception e) { log.warn("获取zk锁异常:{}", e.getMessage()); return false; } } private  ZookeeperLockUtil(); boolean lock(long seckillId); boolean releaseLock(long seckillId);  }