你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

CancellationToken Class

  • java.lang.Object
    • AutoCloseable
      • system.fabric.CancellationToken

public class CancellationToken

A CancellationToken is used to propagate notification that operations should be canceled. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its getToken() method. You then pass the cancellation token to any number of operations that should receive notice of cancellation. The token cannot be used to initiate cancellation. When the owning object calls cancel(), the cancelled property on the cancellation token is set to true. The objects that receive the notification can respond in whatever manner is appropriate.

Method Summary

Modifier and Type Method and Description
boolean canBeCancelled()

Specifies if the Cancellation token can be cancelled or not. If the system.fabric.CancellationToken is created using getToken(), then the token can cancelled using cancel() method. If the system.fabric.CancellationToken is created using getDefault(), then it can not be cancelled. It simply acts as a dummy token.

void close()
CancellationToken getDefault()

Creates and returns a Cancellation token object. This method should be used when the CancellationToken doesn't support cancellation. Only Cancellation tokens created through getToken() supports cancellations.

boolean isCancelled()

Gets whether cancellation has been requested for this token.

void registerCancellationCallback(Runnable callback)

Registers a callback which will be executed when this system.fabric.CancellationToken object is cancelled.

void registerCancellationCallback(String name, Runnable callback)

Registers a callback which will be executed when this system.fabric.CancellationToken object is cancelled.

void throwIfCancellationRequested()

Throws a CancellationException if this token has had cancellation requested.

void unregisterCancellationCallback()

unregister the callback for this system.fabric.CancellationToken object.

void unregisterCancellationCallback(String name)

unregister the callback for this system.fabric.CancellationToken object.

Method Details

canBeCancelled

public boolean canBeCancelled()

Specifies if the Cancellation token can be cancelled or not. If the system.fabric.CancellationToken is created using getToken(), then the token can cancelled using cancel() method. If the system.fabric.CancellationToken is created using getDefault(), then it can not be cancelled. It simply acts as a dummy token.

Returns:

Returns true if the token can be cancelled, else false.

close

public void close()

getDefault

public static CancellationToken getDefault()

Creates and returns a Cancellation token object. This method should be used when the CancellationToken doesn't support cancellation. Only Cancellation tokens created through getToken() supports cancellations.

Returns:

isCancelled

public boolean isCancelled()

Gets whether cancellation has been requested for this token.

Returns:

boolean cancellation status

registerCancellationCallback

public void registerCancellationCallback(Runnable callback)

Registers a callback which will be executed when this system.fabric.CancellationToken object is cancelled.

Parameters:

callback -

java.lang.Runnable to be executed when on token cancel event.

registerCancellationCallback

public void registerCancellationCallback(String name, Runnable callback)

Registers a callback which will be executed when this system.fabric.CancellationToken object is cancelled.

Parameters:

name - callback name.
callback -

java.lang.Runnable to be executed when on token cancel event.

throwIfCancellationRequested

public void throwIfCancellationRequested()

Throws a CancellationException if this token has had cancellation requested.

unregisterCancellationCallback

public void unregisterCancellationCallback()

unregister the callback for this system.fabric.CancellationToken object.

unregisterCancellationCallback

public void unregisterCancellationCallback(String name)

unregister the callback for this system.fabric.CancellationToken object.

Parameters:

name - callback name registered for this system.fabric.CancellationToken object.

Applies to