public class ThreadUtils extends Object
Threads a little bit more convenient.
Apart from offering method to spawn and terminate Threads, it also offers methods to sleep(int)
while correctly maintaining the Thread.isInterrupted() flags.| Constructor and Description |
|---|
ThreadUtils() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
sleep(int timeoutInMS)
This method is a wrapper for the
Thread.sleep(long) method, that correctly handles
InterruptedExceptions and the Thread.interrupted() flag of the calling Thread. |
static Thread |
spawnThread(Runnable runnable,
String threadName)
This method spawns a new
Thread with the given name. |
static Thread |
spawnThread(Runnable runnable,
ThreadIdentifier threadIdentifier)
This method spawns a new
Thread for the given ThreadIdentifier. |
static Thread |
stopThread(ThreadIdentifier threadIdentifier)
This method stops the
Thread that is associated with the given ThreadIdentifier. |
public static Thread spawnThread(Runnable runnable, ThreadIdentifier threadIdentifier)
Thread for the given ThreadIdentifier.
This method is thread safe and makes sure there is exactly one "non-interrupted" Thread associated to the
identifier.
It can therefore be used as a convenient way to offer a start() method in the corresponding manager
instance, without having to worry about starting multiple Threads when calling the start() method
multiple times.runnable - method reference that is responsible for processing the thread (i.e. this::myThreadMethod)threadIdentifier - identifier object that is used to associate the Thread and synchronize the accesspublic static Thread spawnThread(Runnable runnable, String threadName)
Thread with the given name.
This method does not actively manage the Threads and calling it multiple times will create multiple
Threads. It is internally used by spawnThread(Runnable, ThreadIdentifier).
In addition to creating the Thread, it also dumps some log messages to inform the user about the creation
and the lifecycle of the Threads.runnable - method reference that is responsible for processing the thread (i.e. this::threadMethod)threadName - the name of the Thread that shall be started startedpublic static Thread stopThread(ThreadIdentifier threadIdentifier)
Thread that is associated with the given ThreadIdentifier.
This method is thread safe and only calls the Thread.interrupt() method once per running Thread.
If no running Thread is associated with the given ThreadIdentifier, it will have no affect.
It can therefore be used as a convenient way to offer a shutdown() method in the corresponding manager
instance, without having to worry about starting multiple Threads when calling the shutdown()
method multiple times.public static boolean sleep(int timeoutInMS)
Thread.sleep(long) method, that correctly handles
InterruptedExceptions and the Thread.interrupted() flag of the calling Thread.
It first checks if the Thread was interrupted already and otherwise issues a Thread.sleep(long).
If a InterruptedException is caught, it resets the interrupted flag and returns the corresponding result.
See Handle InterruptedExceptiontimeoutInMS - milliseconds that the current Thread should waitCopyright © 2019. All rights reserved.