MCPcopy Create free account
hub / github.com/BoltsFramework/Bolts-Java / TaskCompletionSource

Class TaskCompletionSource

Bolts/src/bolts/Task.java:555–640  ·  view source on GitHub ↗

Allows safe orchestration of a task's completion, preventing the consumer from prematurely completing the task. Essentially, it represents the producer side of a Task , providing access to the consumer side through the getTask() method while isolating the Task's completion mechanisms from th

Source from the content-addressed store, hash-verified

553 * mechanisms from the consumer.
554 */
555 public class TaskCompletionSource {
556 private TaskCompletionSource() {
557 }
558
559 /**
560 * @return the Task associated with this TaskCompletionSource.
561 */
562 public Task<TResult> getTask() {
563 return Task.this;
564 }
565
566 /**
567 * Sets the cancelled flag on the Task if the Task hasn't already been completed.
568 */
569 public boolean trySetCancelled() {
570 synchronized (lock) {
571 if (complete) {
572 return false;
573 }
574 complete = true;
575 cancelled = true;
576 lock.notifyAll();
577 runContinuations();
578 return true;
579 }
580 }
581
582 /**
583 * Sets the result on the Task if the Task hasn't already been completed.
584 */
585 public boolean trySetResult(TResult result) {
586 synchronized (lock) {
587 if (complete) {
588 return false;
589 }
590 complete = true;
591 Task.this.result = result;
592 lock.notifyAll();
593 runContinuations();
594 return true;
595 }
596 }
597
598 /**
599 * Sets the error on the Task if the Task hasn't already been completed.
600 */
601 public boolean trySetError(Exception error) {
602 synchronized (lock) {
603 if (complete) {
604 return false;
605 }
606 complete = true;
607 Task.this.error = error;
608 lock.notifyAll();
609 runContinuations();
610 return true;
611 }
612 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected