Package de.linusdev.lutils.async
Interface Task<R,S>
- Type Parameters:
R- the result type. Nullable.S- the secondary result type. usually contains information about the task's execution process. NotNull.
- All Superinterfaces:
HasAsyncManager
- All Known Subinterfaces:
ConditionedTask<R,,S> ExecutableTask<R,,S> PTask<R,,S> Queueable<T,R>
- All Known Implementing Classes:
CompletableTask,ExecutableTaskBase,QueueableBase
A Task can be executed asynchronous or in the current thread.
Example
asynchronous execution:
Task<String, Nothing> task = ...;
//Queue and create a listener.
//This creates a Future.
task.queue((result, response, error) -> {
if(error != null) {
System.out.println("could not execute task.");
//Handle error
return;
}
//Process result...
System.out.println("Result: " + result);
});
For Information on how to create your own Task, see ExecutableTaskBase,
QueueableBase and CompletableTask.
- See Also:
-
Method Summary
Modifier and TypeMethodDescription@NotNull ComputationResult<R,S> Executes this task in the current thread.default @NotNull StringgetName()queue()Queues theTaskfor future asynchronous execution.queue(@NotNull ResultAndErrorConsumer<R, S> consumer) queue(@NotNull ResultConsumer<R, S> consumer) queue(@NotNull SingleResultConsumer<R, S> consumer) queueAndSetBeforeExecutionListener(@NotNull Consumer<Future<R, S>> consumer) queueAndWriteToFile(@NotNull Path file, boolean overwriteIfExists, @Nullable ResultAndErrorConsumer<R, S> after) What exactly will be written to the file, depends on the implementation.Methods inherited from interface de.linusdev.lutils.async.manager.HasAsyncManager
getAsyncManager
-
Method Details
-
getName
- Returns:
Stringname of the task.
-
executeHere
@NotNull @NotNull ComputationResult<R,S> executeHere() throws InterruptedException, UnsupportedOperationException, CannotQueueTaskException, NonBlockingThreadException- Returns:
ComputationResultresult.- Throws:
NonBlockingThreadException- if the current thread is a thread ofAsyncManagerand should not be blocked. SeeAsyncManager.checkThread().InterruptedException- if the thread got interrupted while executingUnsupportedOperationException- if thisTaskcannot be executed (It may have to wait on some kind of external event)CannotQueueTaskException- if thisTaskcannot be executed for some reason (For Example if it was already executed and cannot be executed again)- See Also:
-
queue
Queues theTaskfor future asynchronous execution. That does not mean, that theTaskactually ends up in a queue, but that theTaskwill be executed at any point in time in the future.- Returns:
Future- Throws:
CannotQueueTaskException- if thisTaskcannot be queued.- See Also:
-
queueAndSetBeforeExecutionListener
@NotNull @NotNull Future<R,S> queueAndSetBeforeExecutionListener(@NotNull @NotNull Consumer<Future<R, S>> consumer) throws CannotQueueTaskException- Parameters:
consumer-Consumerlistener to be called before execution starts.- Returns:
Future- Throws:
CannotQueueTaskException- if thisTaskcannot be queued.- See Also:
-
queue
@NotNull @NotNull Future<R,S> queue(@NotNull @NotNull ResultConsumer<R, S> consumer) throws CannotQueueTaskException- Parameters:
consumer-ResultConsumerlistener to be called when the result is ready.- Returns:
Future- Throws:
CannotQueueTaskException- if thisTaskcannot be queued.- See Also:
-
queue
@NotNull @NotNull Future<R,S> queue(@NotNull @NotNull SingleResultConsumer<R, S> consumer) throws CannotQueueTaskException- Parameters:
consumer-SingleResultConsumerlistener to be called when the result is ready.- Returns:
Future- Throws:
CannotQueueTaskException- if thisTaskcannot be queued.- See Also:
-
queue
@NotNull @NotNull Future<R,S> queue(@NotNull @NotNull ResultAndErrorConsumer<R, S> consumer) throws CannotQueueTaskException- Parameters:
consumer-ResultAndErrorConsumerlistener to be called when the result is ready.- Returns:
Future- Throws:
CannotQueueTaskException- if thisTaskcannot be queued.- See Also:
-
queueAndWait
- Returns:
Taskresult- Throws:
InterruptedException- if interrupted while waitingCannotQueueTaskException- if thisTaskcannot be queued.ErrorException- if theFuturereturned with an error.
-
queueAndWriteToFile
@NotNull @NotNull Future<R,S> queueAndWriteToFile(@NotNull @NotNull Path file, boolean overwriteIfExists, @Nullable @Nullable ResultAndErrorConsumer<R, S> after) throws CannotQueueTaskExceptionWhat exactly will be written to the file, depends on the implementation.- Parameters:
file-Pathof the file to write to.overwriteIfExists- if the file should be overwritten if it already exists.after-ResultAndErrorConsumerto consume the result after it has been written to the file.- Returns:
Future- Throws:
CannotQueueTaskException- if thisTaskcannot be queued.
-