Class SuspendingExecutor

java.lang.Object
com.vaadin.featurepack.desktop.threads.SuspendingExecutor
All Implemented Interfaces:
AutoCloseable

public final class SuspendingExecutor extends Object implements AutoCloseable
"Chops" the execution of the runnable into smaller parts (called Continuations) and run them on given carrier thread executor. Runs on Java Virtual Threads (AKA Project Loom).

Whenever the Runnable execution blocks in a way that it unmounts (see Blocking and unmounting for more info), it "suspends" - the virtual thread stops execution and unmounts from the carrier thread, allowing the carrier thread Runnable to finish. When the Runnable execution unblocks, a new continuation is passed into the carrier thread executor, and the virtual thread mounts on top of a carrier thread.
  • Constructor Details

    • SuspendingExecutor

      public SuspendingExecutor(Executor executor)
      Creates the suspending executor.
      Parameters:
      executor - Executes given Runnables (Continuations) on an actual OS thread (called a carrier thread). No magic happens in this executor - the runnables are run until they're terminated.
  • Method Details

    • run

      public void run(Runnable runnable)
      Runs given runnable in a virtual thread. The actual code execution is "split into smaller pieces" (called continuations) and executed on the carrier thread executor.
      Parameters:
      runnable - the runnable, not null. Run in a virtual thread.
    • close

      public void close()
      Closes the executor immediately. Any suspended virtual threads are killed immediately, then garbage-collected eventually.
      Specified by:
      close in interface AutoCloseable