Class VaadinSuspendingExecutor

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

public final class VaadinSuspendingExecutor extends Object implements AutoCloseable
Executes Runnables in Vaadin UI thread, but in a very special way: when the code blocks, all UI changes are rendered in the browser, and the UI thread is not blocked. You can wait for a button click and unblock the code block to execute further.

Runs on Java Virtual Threads (AKA Project Loom).
  • Constructor Details

    • VaadinSuspendingExecutor

      public VaadinSuspendingExecutor(com.vaadin.flow.component.UI ui)
  • Method Details

    • run

      public void run(Runnable runnable)
      You can call this from anywhere, even from a background thread. Runs given Runnable with the Vaadin session lock held. The Runnable is run in a virtual thread, which means that the runnable may block. When it does, all changes done to the Vaadin components are transmitted to the client-side. Upon unblocking, the runnable continues its execution in the Vaadin UI thread.
      Parameters:
      runnable - the code block to run.
    • 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
    • assertUIVirtualThread

      public static void assertUIVirtualThread()
      Asserts that this thread is a virtual thread which is run in the Vaadin UI thread. It's useful to assert this before attempting to block, to make sure the blocking operation suspends current virtual thread instead.