java.lang.Object
org.newsclub.net.unix.ThreadUtil
Helper class to support certain Thread-specific features.
- Author:
- Christian Kohlschütter
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Checks if the current Thread has been interrupted, without clearing the flag; if interrupted, anInterruptedIOException
is thrown, otherwisetrue
is returned.static boolean
Checks if the currentThread
is to be considered a virtual thread.static boolean
Checks if virtual threads are considered to be supported (and therefore if special support should be enabled).static ExecutorService
Returns a new "virtual thread per task executor".static void
Ensures that the given operation is being executed on a system thread.static void
setTreatAsVirtualThread
(boolean b) Marks the current platformThread
to be treated as a virtual thread, if possible.static Thread
startNewDaemonThread
(boolean virtual, Runnable run) Starts a new daemon thread.
-
Method Details
-
setTreatAsVirtualThread
public static void setTreatAsVirtualThread(boolean b) Marks the current platformThread
to be treated as a virtual thread, if possible. Has no effect if the current Thread already is a virtual thread.- Parameters:
b
-true
to enable treatment of a platform thread as a virtual thread.
-
isVirtualThread
public static boolean isVirtualThread()Checks if the currentThread
is to be considered a virtual thread.- Returns:
true
if so.
-
isVirtualThreadSupported
public static boolean isVirtualThreadSupported()Checks if virtual threads are considered to be supported (and therefore if special support should be enabled).- Returns:
true
if so.
-
newVirtualThreadPerTaskExecutor
Returns a new "virtual thread per task executor". If virtual threads are not supported by this JVM, a new platform thread are created for each task, and such threads are marked to be treated as virtual threads.- Returns:
- The new executor service.
-
checkNotInterruptedOrThrow
Checks if the current Thread has been interrupted, without clearing the flag; if interrupted, anInterruptedIOException
is thrown, otherwisetrue
is returned.- Returns:
true
.- Throws:
InterruptedIOException
- if interrupted.
-
startNewDaemonThread
Starts a new daemon thread.- Parameters:
virtual
- Iftrue
, try to start a virtual Thread instead of a platform Thread (or at least pretend it's a virtual thread if they're not supported natively); iffalse
, a "daemon" platform thread is started.run
- The runnable.- Returns:
- The thread.
-
runOnSystemThread
Ensures that the given operation is being executed on a system thread. If the current thread is a virtual thread, the operation is executed synchronously viaCompletableFuture.runAsync(Runnable)
: the virtual thread is suspended during that operation, and subsequently resumed.- Parameters:
op
- The operation to run.- Throws:
InterruptedException
- on interrupt.
-