Class CancellationToken


  • public final class CancellationToken
    extends java.lang.Object
    Type representing a token to cancel one or more operations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static CancellationToken NONE
      An empty CancellationToken that cannot be cancelled.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel()
      Communicates a request for cancellation.
      boolean isCancellationRequested()
      Gets whether cancellation has been requested for this token by calling cancel().
      void registerOnCancel​(java.lang.Runnable onCancel)
      Registers a Runnable that will be called when this CancellationToken is canceled.
      void registerOnCancel​(java.lang.String id, java.lang.Runnable onCancel)
      Registers a Runnable that will be called when this CancellationToken is canceled.
      void unregisterOnCancel​(java.lang.String id)
      Unregister the Runnable that was registered using registerOnCancel(String, Runnable).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NONE

        public static final CancellationToken NONE
        An empty CancellationToken that cannot be cancelled.
    • Constructor Detail

      • CancellationToken

        public CancellationToken()
    • Method Detail

      • cancel

        public void cancel()
        Communicates a request for cancellation.
      • isCancellationRequested

        public boolean isCancellationRequested()
        Gets whether cancellation has been requested for this token by calling cancel().
        Returns:
        true if cancellation has been requested, otherwise.
      • registerOnCancel

        public void registerOnCancel​(java.lang.Runnable onCancel)
        Registers a Runnable that will be called when this CancellationToken is canceled. This operation of registering the Runnable is non-blocking and thread-safe. If the token is already in the cancelled state then the provided Runnable will be immediately executed. This is O(1) in best case and O(k) in worst, where k is number of concurrent threads in case of race.
        Parameters:
        onCancel - The Runnable to be executed when the CancellationToken is canceled.
      • registerOnCancel

        public void registerOnCancel​(java.lang.String id,
                                     java.lang.Runnable onCancel)
        Registers a Runnable that will be called when this CancellationToken is canceled. This operation of registering the Runnable is non-blocking and thread-safe. If the token is already in the cancelled state then the provided Runnable will be immediately executed. This is O(1) in best case and O(k) in worst, where k is number of concurrent threads in case of race in registering Callback.
        Parameters:
        id - The registration id for the Runnable to register.
        onCancel - The Runnable to be executed when the CancellationToken is canceled.
      • unregisterOnCancel

        public void unregisterOnCancel​(java.lang.String id)
        Unregister the Runnable that was registered using registerOnCancel(String, Runnable). This unregister operation is non-blocking and thread-safe.
        Parameters:
        id - The id of the Runnable to unregister.