Note that there are some explanatory texts on larger screens.

plurals
  1. POObserver - BlockingQueue
    text
    copied!<p>I'm using the observer pattern and a BlockingQueue to add some instances. Now in another method I'm using the queue, but it seems take() is waiting forever, even though I'm doing it like this:</p> <pre><code>/** {@inheritDoc} */ @Override public void diffListener(final EDiff paramDiff, final IStructuralItem paramNewNode, final IStructuralItem paramOldNode, final DiffDepth paramDepth) { final Diff diff = new Diff(paramDiff, paramNewNode.getNodeKey(), paramOldNode.getNodeKey(), paramDepth); mDiffs.add(diff); try { mDiffQueue.put(diff); } catch (final InterruptedException e) { LOGWRAPPER.error(e.getMessage(), e); } mEntries++; if (mEntries == AFTER_COUNT_DIFFS) { try { mRunner.run(new PopulateDatabase(mDiffDatabase, mDiffs)); } catch (final Exception e) { LOGWRAPPER.error(e.getMessage(), e); } mEntries = 0; mDiffs = new LinkedList&lt;&gt;(); } } /** {@inheritDoc} */ @Override public void diffDone() { try { mRunner.run(new PopulateDatabase(mDiffDatabase, mDiffs)); } catch (final Exception e) { LOGWRAPPER.error(e.getMessage(), e); } mDone = true; } </code></pre> <p>whereas mDiffQueue is a LinkedBlockingQueue and I'm using it like this:</p> <pre><code>while (!(mDiffQueue.isEmpty() &amp;&amp; mDone) || mDiffQueue.take().getDiff() == EDiff.INSERTED) {} </code></pre> <p>But I think the first expression is checked whereas mDone isn't true, then maybe mDone is set to true (an observer always is multithreaded?), but it's already invoking mDiffQueue.take()? :-/</p> <p>Edit: I really don't get it right now. I've recently changed it to:</p> <pre><code>synchronized (mDiffQueue) { while (!(mDiffQueue.isEmpty() &amp;&amp; mDone)) { if (mDiffQueue.take().getDiff() != EDiff.INSERTED) { break; } } } </code></pre> <p>If I wait in the debugger a little time it works, but it should also work in "real time" since mDone is initialized to false and therefore the while-condition should be true and the body should be executed.</p> <p>If the mDiffQueue is empty and mDone is true it should skip the body of the while-loop (which means the queue isn't filled anymore).</p> <p>Edit: Seems it is:</p> <pre><code>synchronized (mDiffQueue) { while (!(mDiffQueue.isEmpty() &amp;&amp; mDone)) { if (mDiffQueue.peek() != null) { if (mDiffQueue.take().getDiff() != EDiff.INSERTED) { break; } } } } </code></pre> <p>Even though I don't get why the peek() is mandatory.</p> <p>Edit:</p> <p>What I'm doing is iterating over a tree and I want to skip all INSERTED nodes:</p> <pre><code>for (final AbsAxis axis = new DescendantAxis(paramRtx, true); axis.hasNext(); axis.next()) { skipInserts(); final IStructuralItem node = paramRtx.getStructuralNode(); if (node.hasFirstChild()) { depth++; skipInserts(); ... </code></pre> <p>Basically computing the maximum depth or level in the tree without considering nodes which have been deleted in another revision of the tree (for a comparsion Sunburst visualization), but ok, that's maybe out of scope. Just to illustrate that I'm doing something with nodes which haven't been inserted, even if it's just adjusting the maximum depth.</p> <p>regards,</p> <p>Johannes</p>
 

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