JDeferred is a Java Deferred / Promise library.
<dependency>
<groupId>org.jdeferred</groupId>
<artifactId>jdeferred-core</artifactId>
<version>1.0.1</version>
</dependency>
Normally, when using this framework, you would want to do things asynchronously. However, if there is a need to wait for all deferred tasks to finish, you can use Object.wait or Promise.waitSafely methods.
Promise p = dm.when(...)
.done(...)
.fail(...)
synchronized (p)
while (p.isPending()) {
try {
p.wait();
} catch (InterruptedException e) { ... }
}
}
Alternatively, you can use a more simplified shortcut
Promise p = dm.when(...)
.done(...)
.fail(...)
try {
p.waitSafely();
} catch (InterruptedException e) {
...
}