RejectedExecutionException使用ThreadPoolExecutor和AndroidHttpClient

时间:2013-02-21 00:45:00

标签: android android-asynctask threadpoolexecutor

我正在使用自己的ThreadPoolExecutor来了解我的所有任务何时完成。我有一个方法是client.get()调用,并且成功时,它会执行更多的client.get()调用。但是,当我从第一次成功调用client.get()时,我得到java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@414ca470 rejected from java.util.concurrent.ThreadPoolExecutor@414ab1a0[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]。我该如何解决这个错误?

AsyncHttpClient client = new AsyncHttpClient();
int limit = 20;
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(limit);
ThreadPoolExecutor executor = 
                        new ThreadPoolExecutor(limit, limit, 20, TimeUnit.SECONDS, q);

client.setThreadPool(executor);

parseSilo(url, context); // this fires client.get() ... as it encounters urls in JSON feed
executor.shutdown();

while (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
    Log.e(TAG, executor.getTaskCount() + " tasks left");
}
if (executor.isShutdown()) {
     Log.e(TAG, "done");
}

parseSilo方法

public static void parseSilo(String url, final Context context) {
        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String content) {
                Log.i(TAG, "Parsing Silo");
                JsonNode rootNode;

                //json parsing

                // parse collections
                for (Show show : listOfShows) {
                    parseCollections(show.getCollectionUrl()); // another client.get()
                }

            }

        });
}

1 个答案:

答案 0 :(得分:1)

似乎您在client.get()之后发出了executor.shutdown()来电,因此要么在完成所有shutdown来电后延迟get,要么在关机后不再发出新的executor来电向下{{1}}