Module java.base
Package java.lang

Interface Thread.VirtualThreadTask

All Superinterfaces:
Runnable
Enclosing class:
Thread

public static interface Thread.VirtualThreadTask
extends Runnable
The task submitted to a custom scheduler.
API Note:
The follow example creates a scheduler that uses a small set of kernel threads. It prints the name of each virtual thread before executing its task.

     ExecutorService pool = Executors.newFixedThreadPool(4);
     Executor scheduler = (task) -> {
         Thread vthread = ((Thread.VirtualThreadTask) task).thread();
         System.out.println(vthread);
         pool.execute(task);
     };
 
Since:
99
See Also:
Thread.Builder.virtual(Executor)
  • Method Summary

    Modifier and Type Method Description
    Object attach​(Object att)
    Attaches the given object to this task.
    Object attachment()
    Retrieves the current attachment.
    void run()
    Runs the task on the current thread as the carrier thread.
    Thread thread()
    Return the virtual thread that this task was submitted to run
  • Method Details

    • thread

      Thread thread()
      Return the virtual thread that this task was submitted to run
      Returns:
      the virtual thread
    • attach

      Object attach(Object att)
      Attaches the given object to this task.
      Parameters:
      att - the object to attach
      Returns:
      the previously-attached object, if any, otherwise null
    • attachment

      Object attachment()
      Retrieves the current attachment.
      Returns:
      the object currently attached to this task or null if there is no attachment
    • run

      void run()
      Runs the task on the current thread as the carrier thread.

      Invoking this method with the interrupt status set will first clear the interrupt status. Interrupting the carrier thread while running the task leads to unspecified behavior.

      Specified by:
      run in interface Runnable
      Throws:
      IllegalStateException - if the virtual thread is not in a state to run on the current thread
      IllegalCallerException - if the current thread is a virtual thread
      See Also:
      Thread.run()