diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-07-04 09:21:56 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-07-04 09:21:56 +0200 |
commit | 1b0ec2ebd1b4ee0cc980eed369681f14f66c4b90 (patch) | |
tree | d0dba2cb53de8e5fe5b107c8943e22567392b74f /vespajlib | |
parent | d28eab9a181f634005ffa7d792e2bd29c1730fb1 (diff) |
Make curator locks functional within a runtime
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/concurrent/Locks.java | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java b/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java index fcac7f31356..3086c1b6c86 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Locks.java @@ -29,19 +29,25 @@ public class Locks<TYPE> { private final long timeoutMs; - public Locks(int timeout, TimeUnit timeoutUnit) { + /** Create locks with a default timeout */ + public Locks(long timeout, TimeUnit timeoutUnit) { timeoutMs = timeoutUnit.toMillis(timeout); } + /** Locks key. This will block until the key is acquired or the default timeout is reached. */ + public Lock lock(TYPE key) { + return lock(key, timeoutMs, TimeUnit.MILLISECONDS); + } + /** - * Locks key. This will block until the key is acquired. + * Locks key. This will block until the key is acquired or the timeout is reached. * Users of this <b>must</b> close any lock acquired. - * + * * @param key the key to lock * @return the acquired lock * @throws UncheckedTimeoutException if the lock could not be acquired within the timeout */ - public Lock lock(TYPE key) { + public Lock lock(TYPE key, long timeout, TimeUnit timeoutUnit) { try { ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock(true)); boolean acquired = lock.tryLock(timeoutMs, TimeUnit.MILLISECONDS); |