0

私はいくつかのコードを勉強していますが、ここにメソッドがありますが、それが何をするのかよくわかりません:

1) がありますが、なぜRunnableFutureが割り当てられているのですか?FutureTaskFutureTask = 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、そのインターフェースの具体的な実装です。柔軟性を高めるために、変数をこのような抽象型として宣言することをお勧めします。

SortProcessorCallableのコンストラクターで使用するには、インターフェイスを実装する誰かのカスタム クラスである必要がありますFutureTask

ある種のツリーをソートするために、2 つのスレッドが非同期タスクを開始しているようです。

于 2013-05-21T13:59:12.033 に答える