【发布时间】:2013-02-20 20:44:08
【问题描述】:
我正在使用AsyncHttpClient 来获取一些 JSON。我的方法将解析 JSON 并触发另一个 get 请求,所以我实际上不知道有多少线程正在运行。经过一番搜索,我想我可以使用ThreadPoolExecutor 知道我的所有线程何时完成,这样我就可以写入数据库。如果我使用AsyncHttpClient.get(),执行者如何知道我提交了作业?
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");
}
【问题讨论】:
标签: android android-asynctask threadpoolexecutor