LockingVisitors¶
- public class org.apache.commons.lang3.concurrent.locks.LockingVisitors[source]¶
Combines the monitor and visitor pattern to work with
Locks as an alternative to synchronization.The read and write methods use the locks supplied by the visitor. A
LockingVisitors.ReentrantLockVisitoruses one exclusive lock for both methods. ALockingVisitors.ReadWriteLockVisitoruses the underlying read and write locks, while aLockingVisitors.StampedLockVisitoruses its read and writeLockviews. Read operations may run concurrently only when the supplied lock supports shared reads.For example, to use this class with a
ReentrantLock:In single threaded mode, call
reentrantLockVisitor(Object), passing the object to protect. This creates aLockingVisitors.ReentrantLockVisitorTo access the protected object, create a
FailableConsumerlambda. The consumer will receive the object as a parameter while the visitor holds the lock. Then callLockingVisitors.LockVisitor.acceptReadLocked(FailableConsumer), orLockingVisitors.LockVisitor.acceptWriteLocked(FailableConsumer), passing the consumer.Alternatively, to receive a result object, use a
FailableFunctionlambda. To have the function executed, callLockingVisitors.LockVisitor.applyReadLocked(FailableFunction), orLockingVisitors.LockVisitor.applyWriteLocked(FailableFunction).
Example 1: A thread safe logger class using a
LockingVisitors.ReentrantLockVisitor.public class SimpleLogger1 { private final ReentrantLockVisitor<PrintStream> lock; private final PrintStream ps; public SimpleLogger(OutputStream out) { ps = new PrintStream(out); lock = LockingVisitors.reentrantLockVisitor(ps); } public void log(String message) { lock.acceptWriteLocked(ps -> ps.println(message)); } public void log(byte[] buffer) { lock.acceptWriteLocked(ps -> { ps.write(buffer); ps.println(); }); } }
Example 2: A thread safe logger class using a
LockingVisitors.ReadWriteLockVisitor.public class SimpleLogger2 { private final ReadWriteLockVisitor<PrintStream> lock; private final PrintStream ps; public SimpleLogger(OutputStream out) { ps = new PrintStream(out); lock = LockingVisitors.readWriteLockVisitor(ps); } public void log(String message) { lock.acceptWriteLocked(ps -> ps.println(message)); } public void log(byte[] buffer) { lock.acceptWriteLocked(ps -> { ps.write(buffer); ps.println(); }); } }
Example 3: A thread safe logger class using a
StampedLock.public class SimpleLogger3 { private final StampedLockVisitor<PrintStream> lock; private final PrintStream ps; public SimpleLogger(OutputStream out) { ps = new PrintStream(out); lock = LockingVisitors.stampedLockVisitor(ps); } public void log(String message) { lock.acceptWriteLocked(ps -> ps.println(message)); } public void log(byte[] buffer) { lock.acceptWriteLocked(ps -> { ps.write(buffer); ps.println(); }); } }
- Since:
3.11
Constructors¶
Deprecated. Make private in 4.0. |
- @Deprecated public LockingVisitors()[source]¶
Deprecated: TODO Make private in 4.0.
Make private in 4.0.
- See also:
Methods¶
|
Creates a new instance of |
|
|
Creates a new instance of |
|
|
Creates a new instance of |
|
|
Creates a new instance of |
|
|
Creates a new instance of |
- public static <O> LockingVisitors.ReadWriteLockVisitor<O> create(O object, ReadWriteLock readWriteLock)[source]¶
Creates a new instance of
LockingVisitors.ReadWriteLockVisitorwith the given object and lock.- Type parameters:
<O> – The type of the object to protect.
- Parameters:
object – The object to protect.
readWriteLock – The lock to use.
- Returns:
- Since:
3.13.0
- See also:
- public static <O> LockingVisitors.ReentrantLockVisitor<O> create(O object, ReentrantLock reentrantLock)[source]¶
Creates a new instance of
LockingVisitors.ReentrantLockVisitorwith the given object and lock.- Type parameters:
<O> – The type of the object to protect.
- Parameters:
object – The object to protect.
reentrantLock – The lock to use.
- Returns:
- Since:
3.18.0
- See also:
- public static <O> LockingVisitors.ReentrantLockVisitor<O> reentrantLockVisitor(O object)[source]¶
Creates a new instance of
LockingVisitors.ReentrantLockVisitorwith the given object.- Type parameters:
<O> – The type of the object to protect.
- Parameters:
object – The object to protect.
- Returns:
- Since:
3.18.0
- See also:
- public static <O> LockingVisitors.ReadWriteLockVisitor<O> reentrantReadWriteLockVisitor(O object)[source]¶
Creates a new instance of
LockingVisitors.ReadWriteLockVisitorwith the given object.- Type parameters:
<O> – The type of the object to protect.
- Parameters:
object – The object to protect.
- Returns:
- See also:
- public static <O> LockingVisitors.StampedLockVisitor<O> stampedLockVisitor(O object)[source]¶
Creates a new instance of
LockingVisitors.StampedLockVisitorwith the given object.- Type parameters:
<O> – The type of the object to protect.
- Parameters:
object – The object to protect.
- Returns:
- See also: