[]
AutoCloseableTo use a CancellationTokenSource:
getToken() as a parameter.cancel() on this token source.This class implements AutoCloseable and should be used with try-with-resources to ensure proper cleanup of internal resources.
try (CancellationTokenSource cts = new CancellationTokenSource()) {
cts.cancelAfter(Duration.ofSeconds(10));
CancellationToken token = cts.getToken();
workbook.processTemplate(token);
} catch (Exception e) { }
voidcancel()voidcancelAfter(Duration delay) CancellationTokenSource after the specified time span.voidclose()getToken()CancellationTokenSource.It is assumed that the consumers of getToken() will do 'best-effort' attempt to perform cancellation. This method returns immediately and if the cancellation is successful the cancelled operation will throw CancellationException.
try (CancellationTokenSource cts = new CancellationTokenSource()) {
CancellationToken token = cts.getToken();
cts.cancel();
boolean cancellationRequested = token.isCancellationRequested();
}
CancellationTokenSource after the specified time span.
try (CancellationTokenSource cts = new CancellationTokenSource()) {
cts.cancelAfter(Duration.ZERO);
CancellationToken token = cts.getToken();
}
delay - The time span to wait before canceling this CancellationTokenSource. This value can't be null.CancellationTokenSource.
try (CancellationTokenSource cts = new CancellationTokenSource()) {
CancellationToken token = cts.getToken();
}
CancellationTokenSource cts = new CancellationTokenSource();
try {
cts.cancelAfter(Duration.ofSeconds(10));
workbook.processTemplate(cts.getToken());
} finally {
cts.close();
}
close in interface AutoCloseableException - if cleanup fails.