Interface ThreadDispatcher<P,E extends Tickable>
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 dispatcher(ThreadProvider, int)
, or a similar
overload.
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptiondefault void
createPartition
(P partition) default void
deletePartition
(P partition) static <P,
E extends Tickable>
ThreadDispatcher<P, E> dispatcher
(ThreadProvider<P> provider, int threadCount) Creates a new ThreadDispatcher using default thread names (ex.static <P,
E extends Tickable>
ThreadDispatcher<P, E> dispatcher
(ThreadProvider<P> provider, IntFunction<String> nameGenerator, int threadCount) Creates a new ThreadDispatcher using the caller-provided thread name generatornameGenerator
.boolean
isAlive()
Checks if all thetick threads
are alive.default 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)
.default void
removeElement
(E element) void
shutdown()
Shutdowns all thetick threads
.void
signalUpdate
(ThreadDispatcher.Update<P, E> update) Signals an update to the dispatcher.static <P,
E extends Tickable>
ThreadDispatcher<P, E> Creates a single-threaded dispatcher that uses default thread names.void
start()
Starts all thetick threads
.@Unmodifiable List
<TickThread> threads()
Gets the unmodifiable list of TickThreads used to dispatch updates.void
updateAndAwait
(long time) Prepares the update by creating theTickThread
tasks.default void
updateElement
(E element, P partition)
-
Method Details
-
dispatcher
@Contract(pure=true) static <P,E extends Tickable> ThreadDispatcher<P,E> dispatcher(ThreadProvider<P> provider, int threadCount) Creates a new ThreadDispatcher using default thread names (ex. Ms-Tick-n).Remember to start the dispatcher using
start()
- 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
-
dispatcher
@Contract(pure=true) static <P,E extends Tickable> ThreadDispatcher<P,E> dispatcher(ThreadProvider<P> provider, 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.Remember to start the dispatcher using
start()
- 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.Remember to start the dispatcher using
start()
- 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
void updateAndAwait(long time) Prepares the update by creating theTickThread
tasks.- Parameters:
time
- the tick time in nanos
-
refreshThreads
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
default void refreshThreads()Refreshes all thread as perrefreshThreads(long)
, with a timeout ofLong.MAX_VALUE
. -
signalUpdate
Signals an update to the dispatcher.This method is used to notify the dispatcher of changes that need to be processed, such as partition loads, unloads, or element updates.
Updates are processed at the start of each tick, before the actual ticking of elements.
- Parameters:
update
- the update to signal
-
createPartition
-
deletePartition
-
updateElement
-
removeElement
-
start
void start()Starts all thetick threads
.This will throw an
IllegalThreadStateException
if the threads have already been started. -
isAlive
boolean isAlive()Checks if all thetick threads
are alive.- Returns:
- true if all threads are alive, false otherwise
-
shutdown
void shutdown()Shutdowns all thetick threads
.Action is irreversible.
-