Thanks a lot for replay, guys.
To be honest, here my requirements (for example N=2, i=3, X=3):
1. start N process in parallel with different 'i' param; so I need to run 3 tasks starting form 1 to i: task(1), task(2), task(3)
2. execute any tasks X times
3. if an instance finishes sooner, it can proceed to the next task
I've used Executors.newFixedThreadPool(N) and my params are i*X = 9, i.e. [1,2,3,1,2,3,1,2,3].
At this point my
test was ok. I run 9 tasks, and only N=2 threads at time.
The problem is when I've added a new requirement: if a task(i) is ok then it's not necessary to run again for that 'i' but If it fail, I can run again, of course, until 3 times.
I tried to add in my Task a list of 'i' param (List<Integer> completed) where the execution is OK; so I check, in the run method, if that 'i' param is in the list to jump the execution.
But in this case there is the possibility to run 2 tasks with same 'i' param if that 'i' is not in the completed list, and I want to avoid this, for example I want to wait this execution.
Now I don't know if this approach is fine after this new requirement or I have to change approach.