Java Executor: Small tasks or big ones?

Posted by Arash Shahkar on Stack Overflow See other posts from Stack Overflow or by Arash Shahkar
Published on 2012-10-03T21:35:10Z Indexed on 2012/10/03 21:37 UTC
Read the original article Hit count: 239

Consider one big task which could be broken into hundreds of small, independently-runnable tasks. To be more specific, each small task is to send a light network request and decide upon the answer received from the server. These small tasks are not expected to take longer than a second, and involve a few servers in total.

I have in mind two approaches to implement this using the Executor framework, and I want to know which one's better and why.

  1. Create a few, say 5 to 10 tasks each involving doing a bunch of send and receives.
  2. Create a single task (Callable or Runnable) for each send & receive and schedule all of them (hundreds) to be run by the executor.

I'm sorry if my question shows that I'm lazy to test these and see for myself what's better (at least performance-wise). My question, while looking after an answer to this specific case, has a more general aspect. In situations like these when you want to use an executor to do all the scheduling and other stuff, is it better to create lots of small tasks or to group those into a less number of bigger tasks?

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading