This package gives you simple facilties for threading and asynchronous work.
Simple asynchronous task (which is cancellable):
// You can also use createNonDaemon() if you need a normal thread.
AsyncTask.create(() -> {
try {
Thread.sleep(10000);
System.out.println("Look ma, async!");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// You can call #cancel() if you want.
Running sync-critical code:
SyncExecutionQueue queue = new SyncExecutionQueue();
try {
queue.execute(() -> {
// Run your non thread-safe code here.
});
} catch (InterruptedException e) {
e.printStackTrace();
}
Running/Queuing thread-critical code:
ThreadExecutionQueue queue = new ThreadExecutionQueue();
queue.submitTask(() -> {
// All tasks will be executed in order of submission.
});
// You can also use #submitTaskAndWait() or #submitTaskWithPromise().
Using a ThreadExecutionQueue with a SWT Display:
Display display = ...
ThreadExecutionQueue queue = new ThreadExecutionQueue(new ThreadExecutionQueue.Impl() {
@Override
public void getThread() {
return display.getThread();
}
@Override
public void submitTask(Runnable run) {
display.asyncExec(run);
}
});
Promise:
// This resembles Javascript's promise.
new Promise<String>(() -> {
return "world";
})
.then((location) -> {
System.out.printf("Hello %s!\n", location);
})
.except((t) -> {
t.printStackTrace();
});
Replace VERSION_OR_HASH
with the latest version or commit in this repo and make sure to add the Repository to your build system.
Maven
<dependency>
<groupId>co.casterlabs.commons</groupId>
<artifactId>Async</artifactId>
<version>VERSION_OR_HASH</version>
</dependency>
Gradle
dependencies {
implementation 'co.casterlabs.commons:Async:VERSION_OR_HASH'
}
SBT
libraryDependencies += "co.casterlabs.commons" % "Async" % "VERSION_OR_HASH"
Leiningen
:dependencies [[co.casterlabs.commons/Async "VERSION_OR_HASH"]]