Top Description Fields Constructors Methods
org.newsclub.net.unix

public final Class ThreadUtil

extends Object
Class Inheritance
Annotations
@IgnoreJRERequirement
Imports
java.io.InterruptedIOException, java.util.concurrent.CompletableFuture, .ExecutionException, .ExecutorService, .Executors, java.util.concurrent.locks.LockSupport, org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Helper class to support certain Thread-specific features.
Author
Christian Kohlschütter

Field Summary

Modifier and TypeField and Description
private static final ThreadLocal<Boolean>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true.
checkNotInterruptedOrThrow
()

Checks if the current Thread has been interrupted, without clearing the flag; if interrupted, an InterruptedIOException is thrown, otherwise true is returned.

private static boolean

Returns:

true if so.
isTreatAsVirtualThread
()

Checks if the current platform Thread is treated as a virtual one.

public static boolean

Returns:

true if so.
isVirtualThread
()

Checks if the current Thread is to be considered a virtual thread.

public static boolean

Returns:

true if so.
isVirtualThreadSupported
()

Checks if virtual threads are considered to be supported (and therefore if special support should be enabled).

public static ExecutorService

Returns:

The new executor service.
newVirtualThreadPerTaskExecutor
()

Returns a new "virtual thread per task executor".

public static void
runOnSystemThread(Runnable
The operation to run.
op
)

Ensures that the given operation is being executed on a system thread.

public static void
setTreatAsVirtualThread(boolean
true to enable treatment of a platform thread as a virtual thread.
b
)

Marks the current platform Thread to be treated as a virtual thread, if possible.

public static Thread

Returns:

The thread.
startNewDaemonThread
(boolean
If true, 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); if false, a "daemon" platform thread is started.
virtual
,
Runnable
The runnable.
run
)

Starts a new daemon thread.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

TREAT_AS_VIRTUAL_THREADback to summary
private static final ThreadLocal<Boolean> TREAT_AS_VIRTUAL_THREAD

Constructor Detail

ThreadUtilback to summary
private ThreadUtil()

Method Detail

checkNotInterruptedOrThrowback to summary
public static boolean checkNotInterruptedOrThrow() throws InterruptedIOException

Checks if the current Thread has been interrupted, without clearing the flag; if interrupted, an InterruptedIOException is thrown, otherwise true is returned.

Returns:boolean

true.

Exceptions
InterruptedIOException:
if interrupted.
isTreatAsVirtualThreadback to summary
private static boolean isTreatAsVirtualThread()

Checks if the current platform Thread is treated as a virtual one.

Returns:boolean

true if so.

isVirtualThreadback to summary
public static boolean isVirtualThread()

Checks if the current Thread is to be considered a virtual thread.

Returns:boolean

true if so.

isVirtualThreadSupportedback to summary
public static boolean isVirtualThreadSupported()

Checks if virtual threads are considered to be supported (and therefore if special support should be enabled).

Returns:boolean

true if so.

newVirtualThreadPerTaskExecutorback to summary
public static ExecutorService 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:ExecutorService

The new executor service.

runOnSystemThreadback to summary
public static void runOnSystemThread(Runnable op) throws InterruptedException

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 via CompletableFuture#runAsync(Runnable): the virtual thread is suspended during that operation, and subsequently resumed.

Parameters
op:Runnable

The operation to run.

Exceptions
InterruptedException:
on interrupt.
setTreatAsVirtualThreadback to summary
public static void setTreatAsVirtualThread(boolean b)

Marks the current platform Thread to be treated as a virtual thread, if possible. Has no effect if the current Thread already is a virtual thread.

Parameters
b:boolean

true to enable treatment of a platform thread as a virtual thread.

startNewDaemonThreadback to summary
public static Thread startNewDaemonThread(boolean virtual, Runnable run)

Starts a new daemon thread.

Parameters
virtual:boolean

If true, 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); if false, a "daemon" platform thread is started.

run:Runnable

The runnable.

Returns:Thread

The thread.