Interface Acquirable<T>
-
Method Summary
Modifier and TypeMethodDescriptiondefault <R> RLocks the acquirable element, executefunctionsynchronously and unlock the thread.voidChecks if the current thread owns the acquirable element.Gets the thread to which this acquirable element is assigned.booleanisLocal()Gets if the acquirable element is local to this threadbooleanisOwned()Gets if the acquirable element is owned by this thread.local()Retrieves the acquirable value if and only if the element is already present/ticked in the current thread.Gets all theentitiesbeing ticked in the current thread.lock()Returns a newAcquiredobject which will be locked to the current thread.owned()Retrieves the acquirable value if and only if the element is already acquired/owned.static longRetrieve and reset acquiring time.voidLocks the acquirable element, executeconsumersynchronously and unlock the thread.booleanTry to cheaply lock the acquirable element, executeconsumersynchronously and unlock the thread.static <T> Acquirable<T> unassigned(T value) Creates a newAcquirableobject.unwrap()Unwrap the contained object unsafely.
-
Method Details
-
localEntities
-
resetAcquiringTime
Retrieve and reset acquiring time. -
unassigned
Creates a newAcquirableobject.Mostly for internal use, as a
TickThreadhas to be used and properly synchronized.- Type Parameters:
T- the acquirable element type- Parameters:
value- the acquirable element- Returns:
- a new acquirable object
-
lock
Returns a newAcquiredobject which will be locked to the current thread.Useful when your code cannot be done inside a callback and need to be sync. Do not forget to call
Acquired.unlock()once you are done with it.- Returns:
- an acquired object
- Throws:
IllegalStateException- if the acquirable element is not initialized- See Also:
-
local
Retrieves the acquirable value if and only if the element is already present/ticked in the current thread.Useful when you want only want to acquire an element when you are guaranteed to do not access any external thread.
- Returns:
- an optional containing the acquired element if safe
Optional.empty()otherwise
-
isLocal
boolean isLocal()Gets if the acquirable element is local to this thread- Returns:
- true if the element is linked to the current thread
-
owned
Retrieves the acquirable value if and only if the element is already acquired/owned.Useful when you want only want to acquire an element without depending on any more lock.
Less strict than
local()as using an owned element may create contention.- Returns:
- an optional containing the acquired element if safe
Optional.empty()otherwise
-
isOwned
boolean isOwned()Gets if the acquirable element is owned by this thread. Either by being local, or by already being acquired in the current scope.- Returns:
- true if the element is linked to the current thread
-
sync
-
trySync
Try to cheaply lock the acquirable element, executeconsumersynchronously and unlock the thread.Returns false if there is contention.
- Parameters:
consumer- the callback to execute once the element has been safely acquired- Returns:
- true if the consumer was executed, false otherwise
-
applySync
Locks the acquirable element, executefunctionsynchronously and unlock the thread.Free if the element is already present in the current thread, blocking otherwise.
- Parameters:
function- the function to execute once the element has been safely acquired
-
unwrap
T unwrap()Unwrap the contained object unsafely.Should only be considered when thread-safety is not necessary (e.g. comparing positions)
- Returns:
- the unwrapped value
-
assignedThread
@UnknownNullability TickThread assignedThread()Gets the thread to which this acquirable element is assigned.May change to one tick to the next.
- Returns:
- the assigned thread, null if not initialized (likely on the next tick)
-
assertOwnership
void assertOwnership()Checks if the current thread owns the acquirable element.Throws an
AcquirableOwnershipExceptionif not owned.This method is only enabled when assertions are enabled or
ServerFlag.ACQUIRABLE_STRICTis set to true.- Throws:
AcquirableOwnershipException- if the current thread does not own the acquirable element
-