Stay organized with collections Save and categorize content based on your preferences.
public class TaskCompletionSource<TResult>
Provides the ability to create Task
-based APIs.
Use a TaskCompletionSource
to set a result or exception on a Task
returned from an asynchronous API:
public class MarcoPolo { public static Task<String> marco(int delay) { TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>(); new Handler().postDelayed(() -> taskCompletionSource.setResult("polo"), delay); return taskCompletionSource.getTask(); } }
And then your APIs can be used as any other Task
-consuming APIs:
public class MyActivity extends Activity { @Override public void onStart() { super.onStart(); marco(1000).addOnCompleteListener( task -> Log.d(TAG, "got message after one second: " + task.getResult())); } }Summary Public methods
public void setResult(@Nullable TResult result)
Completes the Task with the specified result.
public boolean trySetException(@NonNull Exception e)
Completes the Task with the specified exception, unless the Task has already completed. If the Task has already completed, the call does nothing.
Returnsboolean
true
if the exception was set successfully, false
otherwise
public boolean trySetResult(@Nullable TResult result)
Completes the Task with the specified result, unless the Task has already completed. If the Task has already completed, the call does nothing.
Returnsboolean
true
if the result was set successfully, false
otherwise
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["`TaskCompletionSource` enables the creation of Task-based APIs by setting results or exceptions for asynchronous operations."],["It provides methods like `setResult` and `setException` to complete the associated `Task`."],["`TaskCompletionSource` can be initialized with a `CancellationToken` to allow cancellation of the `Task`."],["`getTask()` retrieves the underlying `Task` object for chaining further operations."],["Methods like `trySetResult` and `trySetException` offer non-throwing alternatives for completing the `Task`."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4