3

ASyncTask を優先して同時に実行しようとしています。

PriorityBlockingQueue を使用して ThreadPoolExecutor を作成しましたが、propper コンパレーターは標準の Runnables でうまく機能します。でも電話するときは

    new Task().executeOnExecutor(threadPool, (Void[]) null);

PriorityBlockingQueue の Comparator は ASyncTask の内部の Runnable (プライベート) (ソース コードでは mFuture と呼ばれます) を受け取るため、コンパレータでは runnable を識別したり、「優先度」の値を読み取ったりすることができません。

どうすれば解決できますか?ありがとう

4

1 に答える 1

6

android.os.AsyncTaskからソース コードを借りて、独自の com.company.AsyncTask 実装を作成します。この実装では、独自のコードで必要なすべてを制御できます。

android.os.AsyncTask には、THREAD_POOL_EXECUTOR と SERIAL_EXECUTOR の 2 つのベイク済みエグゼキュータが付属しています。

private static final BlockingQueue<Runnable> sPoolWorkQueue =
        new LinkedBlockingQueue<Runnable>(10);

/**
 * An {@link Executor} that can be used to execute tasks in parallel.
 */
public static final Executor THREAD_POOL_EXECUTOR
        = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

/**
 * An {@link Executor} that executes tasks one at a time in serial
 * order. This serialization is global to a particular process.
 */
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();

com.company.AsyncTask で、別の PRIORITY_THREAD_POOL_EXECUTOR を作成し、すべての実装をこのクラス (すべての内部フィールドに可視性がある場所) 内にラップし、次のように AysncTask を使用します。

com.company.AsyncTask asyncTask = new com.company.AsyncTask();
asyncTask.setPriority(1);
asyncTask.executeOnExecutor(com.company.AsyncTask.PRIORITY_THREAD_POOL_EXECUTOR, (Void[]) null);

ここで私の回答を確認し、API レベル 11 より前に executeOnExecutor() を機能させるために独自の AsyncTask を作成する方法を確認してください。

于 2012-08-20T22:16:30.087 に答える