Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you could use a bit of research into asynchronous calls. Pausing for a certain number of milliseconds is <em>not</em> a safe way to ensure that your callback has been called, and as you said, making <code>verify</code> a field will not work if the object is shared by multiple threads.</p> <p>One option you could use is a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html" rel="nofollow"><code>Future&lt;Boolean&gt;</code></a> (in this case, <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/SettableFuture.html" rel="nofollow"><code>SettableFuture&lt;Boolean&gt;</code></a> from the Guava libraries is convenient):</p> <pre><code>final SettableFuture&lt;Boolean&gt; verify = new SettableFuture&lt;Boolean&gt;(); p.lookup(lookupKey, new Continuation&lt;PastContent, Exception&gt;() { public void receiveResult(PastContent result) { P2PPKIContent content = (P2PPKIContent) result; if(content !=null){ verify.set(true); } } public void receiveException(Exception result) { System.out.println("Error looking up " + lookupKey); verify.set(false); } }); return verificationQueue.get(); //this will block until verify has been set! </code></pre> <p>A <code>Future&lt;T&gt;</code> is a concurrency construct that will have a value <em>some time in the future</em>. As soon as you ask for its value, however, it will block until it has a value.</p> <h2>Edit</h2> <p>If for some reason you can't pull in Guava, a slightly less intuitive solution is to use a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html" rel="nofollow"><code>BlockingQueue&lt;Boolean&gt;</code></a> with a capacity of 1:</p> <pre><code>final BlockingQueue&lt;Boolean&gt; verificationQueue = new ArrayBlockingQueue&lt;Boolean&gt;(1); p.lookup(lookupKey, new Continuation&lt;PastContent, Exception&gt;() { public void receiveResult(PastContent result) { P2PPKIContent content = (P2PPKIContent) result; if(content !=null){ verificationQueue.put(true); } } public void receiveException(Exception result) { System.out.println("Error looking up " + lookupKey); verificationQueue.put(false); } }); return verify.take(); //blocks until an item has been put on the queue </code></pre> <p>If you're going to do this, it's worth encapsulating the queue in a class with a more readable API.</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. 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