From 2ef7ce67cce5b6e2c2faccbd60aac7ab9c4c7914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=98=BE=E4=BC=98?= <237809796@qq.com> Date: Sat, 15 Apr 2023 14:04:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Azhub.lock=20?= =?UTF-8?q?=EF=BC=88=E4=B8=BA=E6=9B=BF=E6=8D=A2tryLock=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=81=9A=E8=BF=87=E5=BA=A6=E5=87=86=E7=A1=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/zdemo/zhub/ZHubClient.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/com/zdemo/zhub/ZHubClient.java b/src/com/zdemo/zhub/ZHubClient.java index 9a30e65..2a7e6f8 100644 --- a/src/com/zdemo/zhub/ZHubClient.java +++ b/src/com/zdemo/zhub/ZHubClient.java @@ -475,6 +475,24 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer return lock; } + // 为替换 tryLock 方法做过度准确 + public Lock lock(String key, int duration) { + String uuid = Utility.uuid(); + Lock lock = new Lock(key, uuid, duration, this); + lockTag.put(uuid, lock); + + try { + // c.send("lock", key, uuid, strconv.Itoa(duration)) + send("lock", key, uuid, String.valueOf(duration)); + synchronized (lock) { + lock.wait(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + return lock; + } + // ================================================== timer ================================================== private ConcurrentHashMap timerMap = new ConcurrentHashMap();