Understanding Java FixedThreadPool Understanding Java FixedThreadPool multithreading multithreading

Understanding Java FixedThreadPool


Your code is equivalent to

for (int i = 1; i <= 200; i++){    Task t = new Task();    FL.add(ES.submit(t));}

And after the for loop, the constructor of Task has thus been called 200 times, and the code it contains has thus been executed 200 times. Whether the task is submitted to an executor or not is irrelevant: you're invoking a constructor 200 times in a loop, and after each task has been constructed, it's submitted to the executor. The executor is not the one calling the task constructor.


The tasks will be removed one by one from the queue, so as the execution proceed, the Tasks will be removed, and only the result of them will be stored in those Future objects.

So basically in the memory:

3 Threads
200 -> 0 Task
0 -> 200 Future
(with every executed tasks)


You are creating 200 objects using new Task() and those tasks are submitted to executor. The executors hold a reference to this Task object. So, if in the constructor of the Task you are construct and holding resources then all the 200 Task will be holding the resources.

If possible, you can construct and use the resource in the call method of Task if you don't want 200 instances to construct and hold the resources. In that case, only 3 Task at a time will construct and hold the resource.