我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");
}