Create a new context which will cancel itself at the given Deadline. The returned context will cascade cancellation of its parent. Callers may explicitly cancel the returned context prior to the deadline just as for #withCancellation(). If the unit of work completes before the deadli
(Deadline newDeadline, ScheduledExecutorService scheduler)
| 296 | * </pre> |
| 297 | */ |
| 298 | public CancellableContext withDeadline(Deadline newDeadline, ScheduledExecutorService scheduler) { |
| 299 | checkNotNull(newDeadline, "deadline"); |
| 300 | checkNotNull(scheduler, "scheduler"); |
| 301 | Deadline existingDeadline = getDeadline(); |
| 302 | boolean scheduleDeadlineCancellation = true; |
| 303 | if (existingDeadline != null && existingDeadline.compareTo(newDeadline) <= 0) { |
| 304 | // The new deadline won't have an effect, so ignore it |
| 305 | newDeadline = existingDeadline; |
| 306 | scheduleDeadlineCancellation = false; |
| 307 | } |
| 308 | CancellableContext newCtx = new CancellableContext(this, newDeadline); |
| 309 | if (scheduleDeadlineCancellation) { |
| 310 | newCtx.setUpDeadlineCancellation(newDeadline, scheduler); |
| 311 | } |
| 312 | return newCtx; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Create a new context with the given key value set. The new context will cascade cancellation |