I am developing a program that can send http requests to fetch documents.
I have fill a queue with all the requests items:
Queue<RequestItem> requestItems = buildRequest4Docs();
Then,
int threadNum = requestItems.size();
//ExecutorService exs = Executors.newFixedThreadPool(threadNum);
for (int i = 0; i < threadNum; i++) {
ResponseInterface response = new CMSGOResponse();
RequestTask task = new RequestTask(requestItems.poll(), this, response);
task.run();
//exs.execute(new RequestTask(requestItems.poll(), this, response));
}
//exs.shutdown();
I am confused here, in the for loop,does the tasks run simultaneously? Or the tasks run one by one?
Thanks!
In the way you got it now the tasks will be executed one by one. If you uncomment the code you got now as comments and comment the lines
RequestTask task = new RequestTask(requestItems.poll(), this, response);andtask.run();you will get a concurrent execution.So for the concurrent execution it has to look like this:
In addition to that I suggest, that you do not bind the amount of threads created with the
ExecutorServiceto the amount of task you got to work. Connecting it to the amount of processors of the host system is usually a better method. To get the amount of processors use:Runtime.getRuntime().availableProcessors()And in the executor service initialized like this you put the items of your queue. But that works nicely without fetching the total size, rather by polling the
Queueuntil it does not return additional data.The final result of my proposals could look like this: