如何使用java并发编程的ExecutorService? [英] How to use ExecutorService of java concurrent programming?

查看:330
本文介绍了如何使用java并发编程的ExecutorService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码在远程服务器上上传图像。当我在下面使用时,它会在远程服务器上并行上传所有图像。

Iam using below code for uploading images on the remote server.When I use below it is uploading all images cocurrently on remote server.

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);

将所有未来的对象添加到futurelist中,立即从runnable返回(不等待runnable直到完成上传所有图片(后台上传处理都在进行)

adds all the future objects to futurelist immediately returning from runnable(not waiting in runnable until completion of uploading of all images(background uploading processing is going)

但是每当我在上面的代码中取消注释时,成功上传每个图像后它都会从runnable返回。

But whenever i uncommented below line in above code ,after successful uploading of every image it returns from runnable.

singleFuture = execService.submit(lServerRequest.new uploadImages(image.getDataPath(),image.getDisplayName()));
Log.d("","singleFuture -------"+singleFuture.get());
futureList.add(singleFuture);

我的代码是否有任何问题,是否可以一次更多地连接远程服务器或服务器上有任何负载?如何使用并发编程java上传图像?请给我们指导?

is there anything wrong in my code and is it ok to take remote server connection more at a time or any load is on server?How to upload the images by using cocurrent programming java?Please give us guidance?

执行 submit()执行()函数有同样的效果吗?

Do the submit() and execute() functions have the same effect?

推荐答案

当你调用 singleFuture.get()您正在等待操作完成。因此循环将不会继续执行下一个语句,直到返回结果。

When you call singleFuture.get() you are waiting for the operation to complete. So the loop won't continue to execute next statement until this one returns a result.

您需要在第一个循环中提交任务,之后,另一个循环应该迭代你的列表中的结果 future.get()以使其异步

You need to submit your tasks in the first loop and after that, another loop should iterate over the results future.get() on your list to make it async

来自@ andersoj的答案;

From @andersoj's answer;

池大小应与CPU核心相关。不是您手中的图像数量。假设您有2个核心CPU,系数为5(只是我的系数猜测),用于图像上传时间。

The Pool size should be something related to your CPU cores. Not the number of images you have in hand. Say if you have 2 cored CPU, a coefficient of 5 (just my guess of coefficient) for image uploading io time.

POOL_SIZE = NUM​​_OF_CPU_CORE * coeffiecient;

POOL_SIZE = NUM_OF_CPU_CORE*coeffiecient;

这篇关于如何使用java并发编程的ExecutorService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆