Skip to content

Instantly share code, notes, and snippets.

@saturnism
Created September 12, 2013 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturnism/6544744 to your computer and use it in GitHub Desktop.
Save saturnism/6544744 to your computer and use it in GitHub Desktop.
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