Part of the confusion you are having is that you are attempting to start 10**7
tasks. For the sake of experiment, reduce this to some sensible number, say 30
. Your output will now be
0
1
2
...
27
28
29
Then, approximately one second later, something like
2 task finished3 task finished
0 task finished1 task finished
5 task finished4 task finished6 task finished
7 task finished
The text will be all scrambled up, and in my case the newlines usually got printed in batches. This is because calls to print
are not synchronized properly. The next batch will print approximately a second later:
13 task finished
11 task finished9 task finished8 task finished12 task finished
10 task finished
Similar for the third batch. The last batch will only contain the last 6 outputs (24-30):
24 task finished
25 task finished
26 task finished
29 task finished27 task finished
28 task finished
The thing to remember is that tasks are scheduled immediately. That is the purpose of the thread pool. That means that they just get added to a list of things to run later, which is why you see the printout of x
immediately. The tasks are actually run eight at a time, as you would expect. Actually, the tasks after the first batch are started one-by-one as threads become available, but since they all take almost exactly the same amount of time, it appears as though they are running in batches.
You can set up an experiment to see what happens when half of your tasks take 1 second to run and half take 2 seconds. While they will be started immediately in the order you add them to the queue, the threads for the 1 second tasks will become available twice as fast as the ones for 2 second tasks.