Cancel this context and optionally provide a cause (can be null) for the cancellation. This will trigger notification of listeners. It is safe to call this method multiple times. Only the first call will have any effect. Calling cancel(null) is the same as calling #close.
(Throwable cause)
| 811 | * {@code false} if the context was already cancelled. |
| 812 | */ |
| 813 | @CanIgnoreReturnValue |
| 814 | public boolean cancel(Throwable cause) { |
| 815 | boolean triggeredCancel = false; |
| 816 | ScheduledFuture<?> localPendingDeadline = null; |
| 817 | synchronized (this) { |
| 818 | if (!cancelled) { |
| 819 | cancelled = true; |
| 820 | if (pendingDeadline != null) { |
| 821 | // If we have a scheduled cancellation pending attempt to cancel it. |
| 822 | localPendingDeadline = pendingDeadline; |
| 823 | pendingDeadline = null; |
| 824 | } |
| 825 | this.cancellationCause = cause; |
| 826 | triggeredCancel = true; |
| 827 | } |
| 828 | } |
| 829 | if (localPendingDeadline != null) { |
| 830 | localPendingDeadline.cancel(false); |
| 831 | } |
| 832 | if (triggeredCancel) { |
| 833 | notifyAndClearListeners(); |
| 834 | } |
| 835 | return triggeredCancel; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * Notify all listeners that this context has been cancelled and immediately release |