Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure I understand the question correcly, but I usually use a <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html" rel="nofollow"><code>Future&lt;T&gt;</code></a> for these sorts of tasks.</p> <p>Internally the <code>Future&lt;T&gt;</code> implementation uses wait and notify and all of that, but the interface itself becomes rather clean.</p> <pre><code>Future&lt;Double&gt; getTemperature(int id); </code></pre> <p>In your communication code you then can map incoming messages towards unfulfilled futures. If ordering is guaranteed you could do something like</p> <pre><code>class Something { Map&lt;Integer, Queue&lt;Object&gt;&gt; requests; synchronized Future&lt;?&gt; request(int id, Object data) { MyFutureImpl future = new MyFuture(); requests.get(id).add(future); serializeAndSend(id, data); return future; } void serializeAndSend(id, data) {...} synchronized void response(int id, Object data) { MyFutureImpl future = requests.get(id).remove(); future.setValue(data); // This would fulfill the future, and any // threads waiting in a get() will get the // value and continue. } } </code></pre> <p>Where MyFutureImpl is a very basic future-implementation. I'm assuming there's a communications thread that calls <code>response()</code> when a packet is received. I'm also assuming that the <code>serializeAndSend()</code>-function handles the writing to the client or blocks until the write operation can be made or handed off to a communication thread.</p> <p>The use of concurrency-capable map and queue structures could make some <code>synchronization</code>s unnecessary. If there's only ever one outstanding call per id, the Queue becomes unnecessary of course.</p>
    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.
 

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