Entity
- monitored entity type.public class Monitor<Entity> extends Object
Monitor<Queue<String>> outputQueue = new Monitor<>(new LinkedList<String>()); // ... monitoring thread while(true) { // wait for a new string in queue outputQueue.write( queue -> { System.out.println(queue.pull()); // write access lambda must return its argument // to preserve reference to the queue return queue; }, // wake up when the queue is not empty queue -> !queue.isEmpty() ); } // ... somewhere in some other thread outputQueue.write( queue -> { queue.add("Hello Monitor!"); return queue; } );
Modifier | Constructor | Description |
---|---|---|
|
Monitor(Entity entity) |
Create new instance of Monitor initialized by monitored entity.
|
protected |
Monitor(Entity entity,
Lock readLock,
Lock writeLock) |
Create new instance of Monitor initialized by an entity and custom locks.
|
Modifier and Type | Method | Description |
---|---|---|
protected Condition |
getCondition() |
Get a condition variable which is signaled after mutation or change of
monitored entity.
|
protected Entity |
getEntity() |
Get access to monitored entity without any synchronization.
|
protected Lock |
getReadLock() |
Get a lock which is used for shared access.
|
protected Lock |
getWriteLock() |
Get lock which is used for exclusive access.
|
void |
read(Consumer<Entity> consumer) |
Access monitored entity in shared mode.
|
void |
read(Consumer<Entity> consumer,
Predicate<Entity> predicate) |
Wait for monitored entity to mutate or change to a desirable state and
access it in shared mode.
|
boolean |
read(Consumer<Entity> consumer,
Predicate<Entity> predicate,
long time,
TimeUnit unit) |
Wait for monitored entity to mutate or change to a desirable state and
access it in shared mode.
|
void |
set(Entity entity) |
Change monitored entity.
|
protected void |
setEntity(Entity entity) |
Modify monitored entity directly without any synchronization.
|
Entity |
swap(Entity entity) |
Change monitored entity and return the previous value.
|
void |
write(UnaryOperator<Entity> operator) |
Access monitored entity in exclusive mode.
|
void |
write(UnaryOperator<Entity> operator,
Predicate<Entity> predicate) |
Wait for monitored entity to mutate or change to a desirable state and
access it in exclusive mode.
|
boolean |
write(UnaryOperator<Entity> operator,
Predicate<Entity> predicate,
long time,
TimeUnit unit) |
Wait for monitored entity to mutate or change to a desirable state and
access it in exclusive mode.
|
public Monitor(Entity entity)
entity
- monitored entity.protected Monitor(Entity entity, Lock readLock, Lock writeLock)
entity
- monitored entity.readLock
- Custom read lock.writeLock
- Custom write lock. It may be the same instance as
readLock
.ReentrantReadWriteLock
protected Entity getEntity()
setEntity(java.lang.Object)
protected void setEntity(Entity entity)
entity
- the new monitored entity.set(java.lang.Object)
,
getEntity()
protected final Lock getReadLock()
getWriteLock()
protected final Lock getWriteLock()
getReadLock()
protected final Condition getCondition()
public final void read(Consumer<Entity> consumer)
consumer
- access monitored entity. The consumer must not mutate
monitored entity.read(java.util.function.Consumer, java.util.function.Predicate)
,
read(java.util.function.Consumer, java.util.function.Predicate,
long, java.util.concurrent.TimeUnit)
public final void read(Consumer<Entity> consumer, Predicate<Entity> predicate) throws InterruptedException
consumer
- access monitored entity. The consumer must not mutate
monitored entity.predicate
- a desirable state.InterruptedException
- if the current thread is interrupted.read(java.util.function.Consumer)
,
read(java.util.function.Consumer, java.util.function.Predicate,
long, java.util.concurrent.TimeUnit)
public final boolean read(Consumer<Entity> consumer, Predicate<Entity> predicate, long time, TimeUnit unit) throws InterruptedException
consumer
- access monitored entity. The consumer must not mutate
monitored entity.predicate
- a desirable state.time
- the maximum time to wait.unit
- the time unit of the time argument.true
if the monitored entity has been accessed or
false
if the waiting time detectably elapsed before return from
the method.InterruptedException
- if the current thread is interrupted.read(java.util.function.Consumer)
,
read(java.util.function.Consumer, java.util.function.Predicate)
public final void write(UnaryOperator<Entity> operator)
operator
- exclusively access monitored entity. The operator may
mutate monitored entity or replace it with a new one.write(java.util.function.UnaryOperator, java.util.function.Predicate)
,
write(java.util.function.UnaryOperator, java.util.function.Predicate,
long, java.util.concurrent.TimeUnit)
public final void write(UnaryOperator<Entity> operator, Predicate<Entity> predicate) throws InterruptedException
operator
- exclusively access monitored entity. The operator may
mutate monitored entity or replace it with a new one.predicate
- a desirable state.InterruptedException
- if the current thread is interrupted.write(java.util.function.UnaryOperator)
,
write(java.util.function.UnaryOperator, java.util.function.Predicate,
long, java.util.concurrent.TimeUnit)
public final boolean write(UnaryOperator<Entity> operator, Predicate<Entity> predicate, long time, TimeUnit unit) throws InterruptedException
operator
- exclusively access monitored entity. The operator may
mutate monitored entity or replace it with a new one.predicate
- a desirable state.time
- the maximum time to wait.unit
- the time unit of the time argument.true
if the monitored entity has been accessed or
false
if the waiting time detectably elapsed before return from
the method.InterruptedException
- if the current thread is interrupted.write(java.util.function.UnaryOperator)
,
write(java.util.function.UnaryOperator, java.util.function.Predicate)
public final void set(Entity entity)
entity
- new monitored entity.swap(java.lang.Object)
public final Entity swap(Entity entity)
entity
- new monitored entity.set(java.lang.Object)
Copyright © 2011–2018. All rights reserved.