[]
This class is thread-safe. Typically, a CancellationToken is obtained from a CancellationTokenSource via CancellationTokenSource.getToken() and passed to long-running operations such as Workbook.processTemplate(CancellationToken). The operation can then check isCancellationRequested() to determine whether cancellation has been requested.
try (CancellationTokenSource cts = new CancellationTokenSource()) {
CancellationToken token = cts.getToken();
cts.cancelAfter(Duration.ofSeconds(10));
workbook.processTemplate(token);
} catch (Exception e) {
}
boolean
try (CancellationTokenSource cts = new CancellationTokenSource()) {
CancellationToken token = cts.getToken();
cts.cancel();
boolean cancellationRequested = token.isCancellationRequested();
}