Top Description Inners Constructors Methods
org.apache.commons.lang3.concurrent.locks

public Class LockingVisitors

extends Object
Class Inheritance
Imports
java.util.Objects, java.util.concurrent.locks.Lock, .ReadWriteLock, .ReentrantLock, .ReentrantReadWriteLock, .StampedLock, java.util.function.Supplier, org.apache.commons.lang3.builder.AbstractSupplier, org.apache.commons.lang3.function.Failable, .FailableConsumer, .FailableFunction, .Suppliers

Combines the monitor and visitor pattern to work with Locks as an alternative to synchronization.

Locking may be preferable to synchronization or when an application needs a distinction between read access (multiple threads may have read access concurrently) and write access (only one thread may have write access at any given time).

For example, to use this class with a ReentrantLock:

  1. In single threaded mode, call reentrantLockVisitor(Object), passing the object to protect. This creates a LockingVisitors.ReentrantLockVisitor
  2. To access the protected object, create a FailableConsumer lambda. The consumer will receive the object as a parameter while the visitor holds the lock. Then call LockingVisitors.LockVisitor#acceptReadLocked(FailableConsumer), or LockingVisitors.LockVisitor#acceptWriteLocked(FailableConsumer), passing the consumer.
  3. Alternatively, to receive a result object, use a FailableFunction lambda. To have the function executed, call LockingVisitors.LockVisitor#applyReadLocked(FailableFunction), or LockingVisitors.LockVisitor#applyWriteLocked(FailableFunction).

Example 1: A thread safe logger class using a 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 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
LockingVisitors.LockVisitor<
the wrapped object type.
O
,
the wrapped lock type.
L
>

Wraps a domain object and a lock for access by lambdas.

public static class
LockingVisitors.ReadWriteLockVisitor<
The type of the object to protect.
O
>

Wraps a ReadWriteLock and object to protect.

public static class
LockingVisitors.ReentrantLockVisitor<
The type of the object to protect.
O
>

Wraps a ReentrantLock and object to protect.

public static class
LockingVisitors.StampedLockVisitor<
The type of the object to protect.
O
>

Wraps a StampedLock and object to protect.

Constructor Summary

AccessConstructor and Description
public
LockingVisitors()
Deprecated TODO Make private in 4.0.

Make private in 4.0.

Method Summary

Modifier and TypeMethod and Description
public static <
The type of the object to protect.
O
>
LockingVisitors.ReadWriteLockVisitor<O>

Returns:

A new ReadWriteLockVisitor.
create
(final O
The object to protect.
object
,
final ReadWriteLock
The lock to use.
readWriteLock
)

Creates a new instance of ReadWriteLockVisitor with the given object and lock.

public static <
The type of the object to protect.
O
>
LockingVisitors.ReentrantLockVisitor<O>

Returns:

A new ReentrantLockVisitor.
create
(final O
The object to protect.
object
,
final ReentrantLock
The lock to use.
reentrantLock
)

Creates a new instance of ReentrantLockVisitor with the given object and lock.

public static <
The type of the object to protect.
O
>
LockingVisitors.ReentrantLockVisitor<O>

Returns:

A new ReentrantLockVisitor.
reentrantLockVisitor
(final O
The object to protect.
object
)

Creates a new instance of ReentrantLockVisitor with the given object.

public static <
The type of the object to protect.
O
>
LockingVisitors.ReadWriteLockVisitor<O>

Returns:

A new ReadWriteLockVisitor.
reentrantReadWriteLockVisitor
(final O
The object to protect.
object
)

Creates a new instance of ReadWriteLockVisitor with the given object.

public static <
The type of the object to protect.
O
>
LockingVisitors.StampedLockVisitor<O>

Returns:

A new StampedLockVisitor.
stampedLockVisitor
(final O
The object to protect.
object
)

Creates a new instance of StampedLockVisitor with the given object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait