0

我正在研究一些代码,这是一种方法,我不太确定它的作用:

1)有一个RunnableFuture,为什么分配一个FutureTask,为什么不FutureTask = new Futuretask

2) 是什么意思new SortProcessor<E>,是来自Java还是其他类。

public synchronized void sort() {
    if (this.internalState == InternalState.READ) throw new IllegalStateException();

    final RunnableFuture<E> leftFuture = new FutureTask<E>(new SortProcessor<E>(this.leftChild));
    final RunnableFuture<E> rightFuture = new FutureTask<E>(new SortProcessor<E>(this.rightChild));

    new Thread(leftFuture, "left-child").start();
    new Thread(rightFuture, "right-child").start();

    try {
        this.leftCache = leftFuture.get();
        this.rightCache = rightFuture.get();
    } catch (final InterruptedException interrupt) {
        throw new ThreadDeath();
    } catch (final ExecutionException exception) {
        final Throwable cause = exception.getCause();
        if (cause instanceof Error) throw (Error) cause;
        if (cause instanceof RuntimeException) throw (RuntimeException) cause;
        throw new AssertionError();
    }

    if (this.leftCache != null | this.rightCache != null) {
        this.internalState = InternalState.READ;
    }
}
4

1 回答 1

3

RunnableFuture是一个接口,并且FutureTask是该接口的具体实现。将变量声明为像这样的抽象类型以获得更大的灵活性是一种很好的做法。

SortProcessor必须是实现Callable接口的某人的自定义类,以便在构造函数中使用FutureTask

看起来两个线程正在启​​动异步任务以对某种类型的树进行排序

于 2013-05-21T13:59:12.033 回答