Class ThreadUtil

java.lang.Object
org.newsclub.net.unix.ThreadUtil

@IgnoreJRERequirement public final class ThreadUtil extends Object
Helper class to support certain Thread-specific features.
Author:
Christian Kohlschütter
  • Method Details

    • setTreatAsVirtualThread

      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 - true to enable treatment of a platform thread as a virtual thread.
    • isVirtualThread

      public static boolean isVirtualThread()
      Checks if the current Thread is to be considered a virtual thread.
      Returns:
      true if so.
    • isTrulyAVirtualThread

      public static boolean isTrulyAVirtualThread(Thread t)
      Checks if the given Thread is a virtual thread, regardless of setTreatAsVirtualThread(boolean).
      Parameters:
      t - The thread to check.
      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

      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:
      The new executor service.
    • checkNotInterruptedOrThrow

      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:
      true.
      Throws:
      InterruptedIOException - if interrupted.
    • startNewDaemonThread

      public static Thread startNewDaemonThread(boolean virtual, Runnable run)
      Starts a new daemon thread.
      Parameters:
      virtual - 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 - The runnable.
      Returns:
      The thread.
    • runOnSystemThread

      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 - The operation to run.
      Throws:
      InterruptedException - on interrupt.
    • commonPool

      public static ForkJoinPool commonPool()
      Java 7-compatible wrapper for ForkJoinPool.commonPool().
      Returns:
      The common pool.
    • newWorkStealingPool

      public static ExecutorService newWorkStealingPool()
      Java 7-compatible wrapper for Executors.newWorkStealingPool().
      Returns:
      A new pool.