Package net.schmizz.concurrent
Class Event<T extends Throwable>
java.lang.Object
net.schmizz.concurrent.Event<T>
An event can be set, cleared, or awaited, similar to Python's
threading.event
. The key difference is that a
waiter may be delivered an exception of parameterized type T
.
Uses Promise
under the hood.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionEvent
(String name, ExceptionChainer<T> chainer, ReentrantLock lock, LoggerFactory loggerFactory) Creates this event with givenname
, exceptionchainer
, and associatedlock
.Event
(String name, ExceptionChainer<T> chainer, LoggerFactory loggerFactory) Creates this event with givenname
and exceptionchainer
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
await()
Await this event to have a definitetrue
orfalse
value.void
Await this event to have a definitetrue
orfalse
value, fortimeout
duration.void
clear()
Clear this event.void
Deliver the errort
(after chaining) to any present or future waiters.boolean
boolean
inError()
boolean
isSet()
void
lock()
Acquire the lock associated with this event.void
set()
Sets this event to betrue
.toString()
boolean
Await this event to have a definitetrue
orfalse
value, fortimeout
duration.void
unlock()
Release the lock associated with this event.
-
Field Details
-
SOME
-
promise
-
-
Constructor Details
-
Event
Creates this event with givenname
and exceptionchainer
. Allocates a newLock
object for this event.- Parameters:
name
- name of this eventchainer
-ExceptionChainer
that will be used for chaining exceptions
-
Event
public Event(String name, ExceptionChainer<T> chainer, ReentrantLock lock, LoggerFactory loggerFactory) Creates this event with givenname
, exceptionchainer
, and associatedlock
.- Parameters:
name
- name of this eventchainer
-ExceptionChainer
that will be used for chaining exceptionslock
- lock to use
-
-
Method Details
-
set
public void set()Sets this event to betrue
. Short forset(true)
. -
clear
public void clear()Clear this event. A cleared event!isSet()
. -
deliverError
Deliver the errort
(after chaining) to any present or future waiters. -
isSet
public boolean isSet()- Returns:
- whether this event is in a 'set' state. An event is set by a call to
set()
ordeliverError(java.lang.Throwable)
-
await
public void await() throws TAwait this event to have a definitetrue
orfalse
value.- Throws:
T
- if another thread meanwhile informs this event of an error
-
await
Await this event to have a definitetrue
orfalse
value, fortimeout
duration.- Parameters:
timeout
- timeoutunit
- the time unit for the timeout- Throws:
T
- if another thread meanwhile informs this event of an error, or timeout expires
-
tryAwait
Await this event to have a definitetrue
orfalse
value, fortimeout
duration. If the definite value is not available when the timeout expires, returnsfalse
.- Parameters:
timeout
- timeoutunit
- the time unit for the timeout- Throws:
T
- if another thread meanwhile informs this event of an error
-
hasWaiters
public boolean hasWaiters()- Returns:
- whether there are any threads waiting on this event to be set.
-
inError
public boolean inError()- Returns:
- whether this event is in an error state i.e. has been delivered an error.
-
lock
public void lock()Acquire the lock associated with this event. -
unlock
public void unlock()Release the lock associated with this event. -
toString
-