<groupId>es.zaroz.asynctask</groupId>
<artifactId>AsyncTask</artifactId>
<version>1.0</version>VoidTask.run(() -> {
System.out.println("Hello!");
});This creates a new VoidTask - a task that returns nothing - and runs it without waiting for the result.
There are two ways of waiting for a Task to finish.
- Using the
awaitmethod which blocks the current thread until the task is completed
Task task = VoidTask.run(() -> {
System.out.println("Hello!");
});
task.await();- Using the
thenanderrorcallbacks
VoidTask.run(() -> {
System.out.println("Hello!");
}).then(() -> {
System.out.println("Hello task completed!");
}).error(exception -> {
System.out.println("Hello task failed :(");
exception.printStackTrace();
});ValueTask.run(() -> {
return "Todd";
});This creates a new ValueTask that returns a String (Todd) and runs it without waiting for the result.
You can wait for the result of a ValueTask the same way that you would wait for the result of a VoidTask, but ValueTask returns the result in the callback
ValueTask.run(() -> {
return "Todd";
}).then((t, result) -> {
System.out.println("My cat is named: " + result);
});Stay @ Zaroz Network - 2022
We didn't want a bloated library just to use 5% of it.