How to use ExecutorService of java concurrent programming?

后端 未结 2 413
陌清茗
陌清茗 2021-01-15 12:07

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

        
2条回答
  •  梦谈多话
    2021-01-15 12:28

    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.

    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

    From @andersoj's answer;

    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;

提交回复
热议问题