Interface Dispatcher

All Known Implementing Classes:
SwingDispatcher

public interface Dispatcher

Provides access to a dispatch context: a confined execution context which results must be handed back to, and which must not be blocked. On UI platforms this is the UI thread, the Swing event dispatch thread or the Android main looper, but nothing here requires a user interface.

A context is not necessarily a fixed thread. Implementations may bind one to the calling thread for the duration of a request, as web frameworks providing a per-session context do, which is why bound() asks whether a context is bound here rather than which thread is running.

The implementation is provided via ServiceLoader, defaulting to SYNCHRONOUS when none is available.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Dispatcher
    A Dispatcher without a dispatch context, running tasks on the calling thread.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if a dispatch context is bound to the caller, that is, if this is the context results must be handed back to and which must not be blocked.
    Returns an Executor running tasks on the dispatch context, resolved for the caller.
    static Dispatcher
    Returns the Dispatcher implementation, as provided via ServiceLoader, or SYNCHRONOUS in case no implementation is available.
  • Field Details

    • SYNCHRONOUS

      static final Dispatcher SYNCHRONOUS
      A Dispatcher without a dispatch context, running tasks on the calling thread.

      Used when no implementation is available, and useful for testing.

  • Method Details

    • executor

      Executor executor()

      Returns an Executor running tasks on the dispatch context, resolved for the caller.

      Must be called where a context is bound, see bound(), so that implementations binding a context per request or session resolve the right one.

      Returns:
      an Executor running tasks on the dispatch context
    • bound

      boolean bound()

      Returns true if a dispatch context is bound to the caller, that is, if this is the context results must be handed back to and which must not be blocked.

      This reports a fact, not a recommendation. Whether to hand work off on the strength of it is left to the caller, some always do so regardless.

      Returns:
      true if a dispatch context is bound to the caller
    • instance

      static Dispatcher instance()
      Returns the Dispatcher implementation, as provided via ServiceLoader, or SYNCHRONOUS in case no implementation is available.
      Returns:
      the Dispatcher implementation to use