Package net.minestom.server.thread
Class ThreadDispatcher<P>
java.lang.Object
net.minestom.server.thread.ThreadDispatcher<P>
ThreadDispatcher can be used to dispatch updates (ticks) across a number of "partitions" (such as chunks) that
house
Tickable
instances (such as entities). The parallelism of such updates is defined when the dispatcher
is constructed.
It is recommended that Tickable
s being added to a dispatcher also implement AcquirableSource
, as
doing so will allow the user to synchronize external access to them using the Acquirable
API.
Instances of this class can be obtained by calling of(ThreadProvider, int)
, or a similar
overload.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
A data structure which may containTickable
s, and is assigned a singleTickThread
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
createPartition
(P partition) Registers a new partition.void
deletePartition
(P partition) Deletes an existing partition.static <P> @NotNull ThreadDispatcher
<P> of
(@NotNull ThreadProvider<P> provider, int threadCount) Creates a new ThreadDispatcher using default thread names (ex.static <P> @NotNull ThreadDispatcher
<P> of
(@NotNull ThreadProvider<P> provider, @NotNull IntFunction<String> nameGenerator, int threadCount) Creates a new ThreadDispatcher using the caller-provided thread name generatornameGenerator
.void
Refreshes all thread as perrefreshThreads(long)
, with a timeout ofLong.MAX_VALUE
.void
refreshThreads
(long nanoTimeout) Called at the end of each tick to clear removed tickables, refresh the partition linked to a tickable, and partition threads based onThreadProvider.findThread(Object)
.void
removeElement
(@NotNull Tickable tickable) Removes aTickable
.void
shutdown()
Shutdowns all thetick threads
.static <P> @NotNull ThreadDispatcher
<P> Creates a single-threaded dispatcher that uses default thread names.@Unmodifiable @NotNull List
<@NotNull TickThread> threads()
Gets the unmodifiable list of TickThreads used to dispatch updates.void
updateAndAwait
(long time) Prepares the update by creating theTickThread
tasks.void
updateElement
(@NotNull Tickable tickable, P partition) Updates aTickable
, signalling that it is a part ofpartition
.
-
Method Details
-
of
@NotNull public static <P> @NotNull ThreadDispatcher<P> of(@NotNull @NotNull ThreadProvider<P> provider, int threadCount) Creates a new ThreadDispatcher using default thread names (ex. Ms-Tick-n).- Type Parameters:
P
- the dispatcher partition type- Parameters:
provider
- theThreadProvider
instance to be used for defining thread IDsthreadCount
- the number of threads to create for this dispatcher- Returns:
- a new ThreadDispatcher instance
-
of
@NotNull public static <P> @NotNull ThreadDispatcher<P> of(@NotNull @NotNull ThreadProvider<P> provider, @NotNull @NotNull IntFunction<String> nameGenerator, int threadCount) Creates a new ThreadDispatcher using the caller-provided thread name generatornameGenerator
. This is useful to disambiguate custom ThreadDispatcher instances from ones used in core Minestom code.- Type Parameters:
P
- the dispatcher partition type- Parameters:
provider
- theThreadProvider
instance to be used for defining thread IDsnameGenerator
- a function that should return unique names, given a thread indexthreadCount
- the number of threads to create for this dispatcher- Returns:
- a new ThreadDispatcher instance
-
singleThread
Creates a single-threaded dispatcher that uses default thread names.- Type Parameters:
P
- the dispatcher partition type- Returns:
- a new ThreadDispatcher instance
-
threads
Gets the unmodifiable list of TickThreads used to dispatch updates.This method is marked internal to reflect
TickThread
s own internal status.- Returns:
- the TickThreads used to dispatch updates
-
updateAndAwait
public void updateAndAwait(long time) Prepares the update by creating theTickThread
tasks.- Parameters:
time
- the tick time in milliseconds
-
refreshThreads
public void refreshThreads(long nanoTimeout) Called at the end of each tick to clear removed tickables, refresh the partition linked to a tickable, and partition threads based onThreadProvider.findThread(Object)
.- Parameters:
nanoTimeout
- max time in nanoseconds to update partitions
-
refreshThreads
public void refreshThreads()Refreshes all thread as perrefreshThreads(long)
, with a timeout ofLong.MAX_VALUE
. -
createPartition
Registers a new partition.- Parameters:
partition
- the partition to register
-
deletePartition
Deletes an existing partition.- Parameters:
partition
- the partition to delete
-
updateElement
Updates aTickable
, signalling that it is a part ofpartition
.- Parameters:
tickable
- the Tickable to updatepartition
- the partition the Tickable is part of
-
removeElement
Removes aTickable
.- Parameters:
tickable
- the Tickable to remove
-
shutdown
public void shutdown()Shutdowns all thetick threads
.Action is irreversible.
-