Class CompletableTask<R,S>
java.lang.Object
de.linusdev.lutils.async.completeable.CompletableTask<R,S>
- Type Parameters:
R- result typeS- secondary type
- All Implemented Interfaces:
HasAsyncManager,PTask<R,,S> Task<R,S>
A
Example:
Task, which creates a CompletableFuture when started.
Example:
CompletableTask<String, Nothing> task = new CompletableTask<>(asyncManager) {
@Override
public void start(
CompletableFuture<String, Nothing,
CompletableTask<String, Nothing>> future
) {
//Store the future for later completion or start execution...
new Thread(() -> {
try {
//Do work:
Thread.sleep(3000);
//complete the future in a different Thread
future.complete("Hello", Nothing.INSTANCE, null);
} catch (Throwable e) {
future.complete(null, Nothing.INSTANCE,
new ThrowableAsyncError(e));
}
}).start();
}
};
Task<String, Nothing> taskToReturn = task;
//End User:
Future<String, Nothing> f = taskToReturn.queue();
String res = f.getResult();
System.out.println("Result: " + res);- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@NotNull CompletableFuture<R,S, CompletableTask<R, S>> consumeAndQueue(@Nullable Consumer<Future<R, S>> consumer) @NotNull ComputationResult<R,S> Executes this task in the current thread.@NotNull AsyncManagerprotected abstract voidstart(@NotNull CompletableFuture<R, S, CompletableTask<R, S>> future) This method should start the execution of this task.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.linusdev.lutils.async.PTask
queue, queue, queue, queue, queueAndSetBeforeExecutionListener, queueAndWait, queueAndWriteToFile
-
Constructor Details
-
CompletableTask
-
-
Method Details
-
executeHere
Description copied from interface:Task- Specified by:
executeHerein interfaceTask<R,S> - Returns:
ComputationResultresult.- See Also:
-
consumeAndQueue
@NotNull public @NotNull CompletableFuture<R,S, consumeAndQueueCompletableTask<R, S>> (@Nullable @Nullable Consumer<Future<R, S>> consumer) -
start
@NonBlocking protected abstract void start(@NotNull @NotNull CompletableFuture<R, S, throws CannotQueueTaskExceptionCompletableTask<R, S>> future) This method should start the execution of this task. The execution should start in a new thread and not block the current thread. Once the execution has finished, the future must becompletedwith the execution result.
CompletableFuture.startIfNotCanceled()will automatically be called before this method.- Parameters:
future- the future which should becompleted- Throws:
CannotQueueTaskException- if the task cannot be started
-
getAsyncManager
- Specified by:
getAsyncManagerin interfaceHasAsyncManager
-