Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This sounds like a case where you could make good use of <code>Future</code>s and <code>Executor</code>s. See more here:</p> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html" rel="nofollow noreferrer">http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html</a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Executor.html" rel="nofollow noreferrer">http://java.sun.com/javase/6/docs/api/java/util/concurrent/Executor.html</a></li> </ul> <p>Here's a concrete example:</p> <pre><code>ExecutorService executor = Executors.newSingleThreadExecutor(); final Input input = ... Future&lt;Output&gt; future = executor.submit(new Callable&lt;Output&gt;() { public Output call() { return respond(input); } }); // do some work Output output = future.get(); // this blocks until it's done </code></pre> <p>In your case, since you mentioned that some of the fields in the <code>Output</code> object are to be used immediately, while others are used at a later point, you could structure your <code>Output</code> object to contain futures as fields. For example:</p> <pre><code>public class Output { private String fieldA; // immediate field private Future&lt;String&gt; fieldB; // delayed field public void setFieldA(String fieldA) { this.fieldA = fieldA; } public String getFieldA() { return fieldA; } public void setFieldB(Future&lt;String&gt; fieldB) { this.fieldB = fieldB; } public Future&lt;String&gt; getFieldB() { return fieldB; } } </code></pre> <p>You'd then structure your respond method like this:</p> <pre><code>public Output respond(final Input input) { Output output = new Output(); String fieldA = ... output.setFieldA(fieldA); Future&lt;String&gt; fieldBFuture = executor.submit(new Callable&lt;String&gt;() { public String call() { String fieldB = ... return fieldB; } } output.setFieldB(fieldBFuture); } </code></pre> <p>And to use your <code>Output</code> object:</p> <pre><code>Input input = ... Output output = respond(input); String fieldA = output.getFieldA(); // do some work String fieldB = output.getFieldB().get(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload