0
我使用AsyncHttpClient
来获得一些JSON。我的方法将通过JSON进行解析并将触发另一个获取请求,所以实际上我不知道有多少个线程正在运行。经过一番搜索之后,我想我可以使用ThreadPoolExecutor
来了解我的所有线程何时完成,因此我可以写入数据库。如果我使用AsyncHttpClient.get()
,执行者将如何知道我提交了一份工作?AsyncHttpClient与ThreadPoolExecutor知道所有任务何时完成?
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");
}
对,也许我的问题不清楚。如果我使用AsyncHttpClient.get(),执行程序如何知道我提交了一份工作? – heero 2013-02-20 20:51:37
您正在覆盖'AsyncHttpClient'的默认'ThreadPoolExecuter',以便在发出请求时使用它 – iTech 2013-02-20 20:52:41
好吧我认为我现在明白了,但由于AsyncHttpClient.get()是异步调用,所以这样不好吗? – heero 2013-02-20 20:55:02