我使用下面的代码在远程服务器上上传图像。当我使用下面的代码时,它会同时在远程服务器上上传所有图像。
List<Future<String>> futureList = new ArrayList<Future<String>>();
ExecutorService execService = Executors.newFixedThreadPool(Images.size());
for (IImage image : Images) {
try {
//execService.execute(lServerRequest.new uploadImages(image.getDataPath(),image.getDisplayName()));
singleFuture = execService.submit(lServerRequest.new uploadImages(image.getDataPath(),image.getDisplayName()));
//Log.d("","singleFuture -------"+singleFuture.get());
futureList.add(singleFuture);
Log.d("","futureList Size:"+futureList.size());
} catch(Exception e){
execService.shutdown();
}
每当我使用下面的代码时
singleFuture = execService.submit(lServerRequest.new uploadImages(image.getDataPath(),image.getDisplayName()));
//Log.d("","singleFuture -------"+singleFuture.get());
futureList.add(singleFuture);
将所有 future 对象添加到 futurelist 中,立即从 runnable 返回(不在 runnable 中等待,直到完成所有图像的上传(后台上传处理正在进行)
但是每当我取消上面代码中下面一行的注释时,在成功上传每个图像后,它就会从可运行状态返回。
singleFuture = execService.submit(lServerRequest.new uploadImages(image.getDataPath(),image.getDisplayName()));
Log.d("","singleFuture -------"+singleFuture.get());
futureList.add(singleFuture);
我的代码有什么问题吗?一次可以多次连接远程服务器吗?或者服务器上有任何负载吗?如何使用并发编程java上传图像?请给我们指导吗?
Do the submit()
and execute()
功能有同样的效果吗?