android 线程池中的完成回调
Completion Callback in android thread pool
我开发了一个应用程序来读取点类型的 KML 文件,然后使用 Google Elevation API 更新它们的高程。如您所见,它接收点的纬度和经度,并在其后附加 API 键以检索海拔。因为我的 KML 文件有多个点,所以我使用 ThreadPool 读取点的纬度和经度,用键附加它,然后将 URL 发送到 Google 高程 API.Something,如下所示:
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(CORE_NUMBERS + 1);
String providerURL = provider.getServiceURL();
String providerKey = provider.getServiceAPIkey();
for (PointFeature p: points) {
String coordinate = p.getLatitude() + "," + p.getLongitude(); // get latitude and longitude of the feature
String url = providerURL + "locations=" + coordinate + providerKey; // creating the url of web service which contains coordinate
HeightTask task = new HeightTask(url, p); // task of each points
executor.execute(task);
}
heightTask class 是我解析 API 的 JSON 结果并获取高度并设置 heithUpdate 标志的地方。这是片段:
public class HeightTask implements Runnable {
private String url;
private Feature feature;
public HeightTask(String url, Feature f) {
this.feature = f;
this.url = url;
}
@Override
public void run() {
if (feature instanceof PointFeature) {
float height = GoogleAPIJsonParser.parsePoint(HttpManager.getData(url));
if (height != Float.NaN){
feature.updateHeight(height);
feature.setHeightUpdated(true);
Log.d("elevationPoint",height+"");
}
}
}
}
我需要的是一个回调,以了解图层中所有点的高程是否已更新。 threadPool 中是否有任何模式或只是遍历所有点并检查 hieghtUpdate 标志?
修改代码如下。
更改 HeightTask
以实现 Callable 接口。
准备一组可调用任务并使用invokeAll()
提交
List<HeightTask > futureList = new ArrayList<HeightTask >();
for (PointFeature p: points) {
String coordinate = p.getLatitude() + "," + p.getLongitude(); // get latitude and longitude of the feature
String url = providerURL + "locations=" + coordinate + providerKey; // creating the url of web service which contains coordinate
HeightTask task = new HeightTask(url, p);
futureList.add(taks);
}
executor.invokeAll(futureList);
全部调用:
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception.
我开发了一个应用程序来读取点类型的 KML 文件,然后使用 Google Elevation API 更新它们的高程。如您所见,它接收点的纬度和经度,并在其后附加 API 键以检索海拔。因为我的 KML 文件有多个点,所以我使用 ThreadPool 读取点的纬度和经度,用键附加它,然后将 URL 发送到 Google 高程 API.Something,如下所示:
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(CORE_NUMBERS + 1);
String providerURL = provider.getServiceURL();
String providerKey = provider.getServiceAPIkey();
for (PointFeature p: points) {
String coordinate = p.getLatitude() + "," + p.getLongitude(); // get latitude and longitude of the feature
String url = providerURL + "locations=" + coordinate + providerKey; // creating the url of web service which contains coordinate
HeightTask task = new HeightTask(url, p); // task of each points
executor.execute(task);
}
heightTask class 是我解析 API 的 JSON 结果并获取高度并设置 heithUpdate 标志的地方。这是片段:
public class HeightTask implements Runnable {
private String url;
private Feature feature;
public HeightTask(String url, Feature f) {
this.feature = f;
this.url = url;
}
@Override
public void run() {
if (feature instanceof PointFeature) {
float height = GoogleAPIJsonParser.parsePoint(HttpManager.getData(url));
if (height != Float.NaN){
feature.updateHeight(height);
feature.setHeightUpdated(true);
Log.d("elevationPoint",height+"");
}
}
}
}
我需要的是一个回调,以了解图层中所有点的高程是否已更新。 threadPool 中是否有任何模式或只是遍历所有点并检查 hieghtUpdate 标志?
修改代码如下。
更改
HeightTask
以实现 Callable 接口。准备一组可调用任务并使用invokeAll()
提交List<HeightTask > futureList = new ArrayList<HeightTask >(); for (PointFeature p: points) { String coordinate = p.getLatitude() + "," + p.getLongitude(); // get latitude and longitude of the feature String url = providerURL + "locations=" + coordinate + providerKey; // creating the url of web service which contains coordinate HeightTask task = new HeightTask(url, p); futureList.add(taks); } executor.invokeAll(futureList);
全部调用:
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception.