This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new 6363fce Better method names. 6363fce is described below commit 6363fced4b3837afb8a36ed109264b016097d8d8 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jun 19 11:06:52 2020 -0400 Better method names. --- .../org/apache/commons/lang3/concurrent/Locks.java | 23 +++++++++++----------- .../apache/commons/lang3/concurrent/LocksTest.java | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/concurrent/Locks.java b/src/main/java/org/apache/commons/lang3/concurrent/Locks.java index d2cdfdb..28df9c0 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/Locks.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/Locks.java @@ -39,30 +39,30 @@ import org.apache.commons.lang3.function.FailableFunction; * the lock.</li> * <li>If you want to access the locked object, create a {@link FailableConsumer}. The consumer will receive the locked * object as a parameter. For convenience, the consumer may be implemented as a Lambda. Then invoke - * {@link Locks.Lock#runReadLocked(FailableConsumer)}, or {@link Locks.Lock#runWriteLocked(FailableConsumer)}, passing + * {@link Locks.Lock#acceptReadLocked(FailableConsumer)}, or {@link Locks.Lock#acceptWriteLocked(FailableConsumer)}, passing * the consumer.</li> * <li>As an alternative, if you need to produce a result object, you may use a {@link FailableFunction}. This function * may also be implemented as a Lambda. To have the function executed, invoke - * {@link Locks.Lock#callReadLocked(FailableFunction)}, or {@link Locks.Lock#callWriteLocked(FailableFunction)}.</li> + * {@link Locks.Lock#applyReadLocked(FailableFunction)}, or {@link Locks.Lock#applyWriteLocked(FailableFunction)}.</li> * </ol> * * Example: A thread safe logger class. * * <pre> * public class SimpleLogger { + * * private final Lock<PrintStream> lock; * * public SimpleLogger(OutputStream out) { - * PrintStream ps = new PrintStream(out); - * lock = Locks.lock(ps); + * lock = Locks.lock(new PrintStream(out)); * } * * public void log(String message) { - * lock.runWriteLocked((ps) -> ps.println(message)); + * lock.acceptWriteLocked((ps) -> ps.println(message)); * } * * public void log(byte[] buffer) { - * lock.runWriteLocked((ps) -> { ps.write(buffer); ps.println(); }); + * lock.acceptWriteLocked((ps) -> { ps.write(buffer); ps.println(); }); * } * </pre> * @@ -71,26 +71,27 @@ import org.apache.commons.lang3.function.FailableFunction; public class Locks { public static class Lock<O extends Object> { - private final O lockedObject; + private final StampedLock lock = new StampedLock(); + private final O lockedObject; public Lock(final O lockedObject) { this.lockedObject = Objects.requireNonNull(lockedObject, "Locked Object"); } - public void runReadLocked(final FailableConsumer<O, ?> consumer) { + public void acceptReadLocked(final FailableConsumer<O, ?> consumer) { lockAcceptUnlock(() -> lock.readLock(), consumer); } - public void runWriteLocked(final FailableConsumer<O, ?> consumer) { + public void acceptWriteLocked(final FailableConsumer<O, ?> consumer) { lockAcceptUnlock(() -> lock.writeLock(), consumer); } - public <T> T callReadLocked(final FailableFunction<O, T, ?> function) { + public <T> T applyReadLocked(final FailableFunction<O, T, ?> function) { return lockApplyUnlock(() -> lock.readLock(), function); } - public <T> T callWriteLocked(final FailableFunction<O, T, ?> function) { + public <T> T applyWriteLocked(final FailableFunction<O, T, ?> function) { return lockApplyUnlock(() -> lock.writeLock(), function); } diff --git a/src/test/java/org/apache/commons/lang3/concurrent/LocksTest.java b/src/test/java/org/apache/commons/lang3/concurrent/LocksTest.java index ee8fc09..461bb53 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/LocksTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/LocksTest.java @@ -60,9 +60,9 @@ public class LocksTest { }; final Thread t = new Thread(() -> { if (exclusiveLock) { - lock.runWriteLocked(consumer); + lock.acceptWriteLocked(consumer); } else { - lock.runReadLocked(consumer); + lock.acceptReadLocked(consumer); } }); modify(runningValues, i, true);