From 6363fced4b3837afb8a36ed109264b016097d8d8 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 19 Jun 2020 11:06:52 -0400 Subject: [PATCH] Better method names. --- .../commons/lang3/concurrent/Locks.java | 23 ++++++++++--------- .../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 d2cdfdb93..28df9c095 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. *
  • 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.
  • *
  • 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)}.
  • + * {@link Locks.Lock#applyReadLocked(FailableFunction)}, or {@link Locks.Lock#applyWriteLocked(FailableFunction)}. * * * Example: A thread safe logger class. * *
      *   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(); });
      *     }
      * 
    * @@ -71,26 +71,27 @@ import org.apache.commons.lang3.function.FailableFunction; public class Locks { public static class Lock { - 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 consumer) { + public void acceptReadLocked(final FailableConsumer consumer) { lockAcceptUnlock(() -> lock.readLock(), consumer); } - public void runWriteLocked(final FailableConsumer consumer) { + public void acceptWriteLocked(final FailableConsumer consumer) { lockAcceptUnlock(() -> lock.writeLock(), consumer); } - public T callReadLocked(final FailableFunction function) { + public T applyReadLocked(final FailableFunction function) { return lockApplyUnlock(() -> lock.readLock(), function); } - public T callWriteLocked(final FailableFunction function) { + public T applyWriteLocked(final FailableFunction 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 ee8fc09f4..461bb539e 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);