开发者

List of Future-Instances

开发者 https://www.devze.com 2023-04-02 07:46 出处:网络
I\'d like to replace a List of Future-Instances with something more performant. Currently I\'m traversing a tree and submit a Callable to determine the number of descendant-or-self nodes for each node

I'd like to replace a List of Future-Instances with something more performant. Currently I'm traversing a tree and submit a Callable to determine the number of descendant-or-self nodes for each node in the tree. I'm saving the Future instance in a List and then get the appropriate node count from the List once required:

try {
    assert mIndex + 1 < mDescendants.size();
    mItem =
        Item.BUILDER.set(mAngle, mExtension, mIndexToParent).setParentDescendantCount(
                mParDescendantCount).setDescendantCount(mDescendants.get(mIndex + 1).get()).build();
} catch (final InterruptedException | ExecutionException e) {
    LOGWRAPPER.error(e.getMessage(), e);
}

The sad thing is that the axis which is making use of the List has to wait until all Future instances have been submitted. Furthermore it doesn't scale beyond main memory limits :-/

Maybe Google Guava and ListenableFuture is the right thing to use.

Edit: Now I think I'll actually build something with a PropertyChangeListener where Futures are added to a list whenever a Future is fired. Then I initiate a CountDownLatch to 1 and call countDown() everytime a new Future is added to the List. Something like:

/**
 * {@inheritDoc}
 */
@Override
public boolean hasNext() {
    if (mDescendants.size() > 0) {
        return doHasNext();
    } else {
        try {
            mLatch.await(5, TimeUnit.SECONDS);
        } catch (final InterruptedException e) {
            LOGWRAPPER.error(e.getMessage(), e);
        }
        return doHasNext();
    }
}

then in doHasNext():

try {
    assert mIndex + 1 < mDescendants.size();
    mItem =
        Item.BUILDER.set(mAngle, mExtension, mIndexToParent).setParentDescendantCount(
                mParDescendantCount).setDescendantCount(mDescendants.get(mIndex + 1).get()).build();
    mLatch = new CountDownLatch(1);
} catch (final InterruptedException | ExecutionException e) {
    LOGWRAPPER.error(e.getMessage(), e);
}

and the Listener:

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public void propertyChange(final PropertyChangeEvent paramEvent) {
    Objects.requireNonNull(paramEvent);

    if ("desc开发者_高级运维endants".equals(paramEvent.getPropertyName())) {
        mDescendants.add((Future<Integer>) paramEvent.getNewValue());
        mLatch.countDown();
    }
}

I'm not sure if it works, it's too late and I mistrust the way I would use the CountDownLatch (haven't tested the above code).

Edit: Just in case someone is interested. Instead of the CountDownLatch and a List I now simply used a BlockingQueue in conjunction with the implementation of a PropertyChangeListener, which seems to be a good, "clean" solution.

regards,

Johannes


Can't you just use a completion service? Once submitted, it'll process the first future to complete...

0

精彩评论

暂无评论...
验证码 换一张
取 消