Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JDeferred 1.0.1 Released! Checkout http://jdeferred.org for updated examples, including Async Servlet and Java 8 Lambda usage.

JDeferred is a Java Deferred / Promise library.

JDeferred Maven Artifact

<dependency>
    <groupId>org.jdeferred</groupId>
    <artifactId>jdeferred-core</artifactId>
    <version>1.0.1</version>
</dependency>

Wait and WaitSafely

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) {
  ... 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment