我认为queryFeaturesAsync()
里面的方法已经使用ThreadPoolService
,并且使用ThreadPoolService
从另一个检索结果ThreadPoolService
是矫枉过正,恕我直言。
我会推荐你使用AsyncTask
,因为它是原生的 Android SDK 工具并且使用起来非常简单:
AsyncTask<ListenableFuture<FeatureQueryResult>, Integer, List<FeatureQueryResult>> task =
new AsyncTask<ListenableFuture<FeatureQueryResult>, Integer, List<FeatureQueryResult>>() {
@Override
protected List<FeatureQueryResult> doInBackground(ListenableFuture<FeatureQueryResult>[] futures) {
int counter = 0;
List<FeatureQueryResult> results = new ArrayList<>();
for (ListenableFuture<FeatureQueryResult> future : futures) {
try {
results.add(future.get());
publishProgress(++counter); // Optional feature
} catch (ExecutionException | InterruptedException e) {
Log.w("Huh?", "Interrupted!");
}
}
return results;
}
@Override
protected void onProgressUpdate(Integer... values) {
notifyMainThreadAboutProgress(values[0]); // Optional feature
}
@Override
protected void onPostExecute(List<FeatureQueryResult> featureQueryResults) {
doSomethingWithResultsInMainThread(featureQueryResults);
}
};
ListenableFuture<FeatureQueryResult> future1 = gdbFeatureTable.queryFeaturesAsync(query);
ListenableFuture<FeatureQueryResult> future2 = gdbFeatureTable.queryFeaturesAsync(another_query);
ListenableFuture<FeatureQueryResult> future3 = gdbFeatureTable.queryFeaturesAsync(yet_another_query);
task.execute(future1, future2, future3);
我AsyncTask
作为一个匿名类来缩短示例。在生产中,它必须是静态类。