Package net.minestom.server.thread
Interface Acquirable<T>
@Experimental
public sealed interface Acquirable<T>
-
Method Summary
Modifier and TypeMethodDescription@NotNull TickThread
Gets all theentities
being ticked in the current thread.default boolean
isLocal()
Gets if the acquirable element is local to this threadlocal()
Retrieves the acquirable value if and only if the element is already present/ticked in the current thread.lock()
Returns a newAcquired
object which will be locked to the current thread.static <T> @NotNull Acquirable
<T> of
(T value) Creates a newAcquirable
object.static long
Retrieve and reset acquiring time.default void
Locks the acquirable element, executeconsumer
synchronously and unlock the thread.unwrap()
Unwrap the contained object unsafely.
-
Method Details
-
currentEntities
Gets all theentities
being ticked in the current thread.Useful when you want to ensure that no acquisition is ever done.
Be aware that the entity stream is only updated at the beginning of the thread tick.
- Returns:
- the entities ticked in the current thread
-
resetAcquiringTime
@Internal static long resetAcquiringTime()Retrieve and reset acquiring time. -
of
Creates a newAcquirable
object.Mostly for internal use, as a
TickThread
has 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 newAcquired
object 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
- 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 create a huge performance impact.
- Returns:
- an optional containing the acquired element if safe,
Optional.empty()
otherwise
-
isLocal
default boolean isLocal()Gets if the acquirable element is local to this thread- Returns:
- true if the element is linked to the current thread
-
sync
Locks the acquirable element, executeconsumer
synchronously and unlock the thread.Free if the element is already present in the current thread, blocking otherwise.
- Parameters:
consumer
- the callback to execute once the element has been safely acquired
-
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
-