Skip to content

ZarozNetwork/AsyncTask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AsyncTask

Java 1.8 library that facilitates asynchronous programming.

<groupId>es.zaroz.asynctask</groupId>
<artifactId>AsyncTask</artifactId>
<version>1.0</version>

Examples

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.

  1. Using the await method which blocks the current thread until the task is completed
Task task = VoidTask.run(() -> {
    System.out.println("Hello!");
});
task.await();
  1. Using the then and error callbacks
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);
});

About

Stay @ Zaroz Network - 2022

Why does this exist? Why don't use an established library?

We didn't want a bloated library just to use 5% of it.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages