Go up to Implementation Go forward to Garbage Collection |
As an example, let us take a look at a program where the initial thread creates two threads and waits for their results in turn. Each new thread creates two other threads resulting in the tree of threads of depth 4 depicted in the following figure:
Assume that there are three processes executing the program. A possible execution schedule is then described by the following table:
At time 0, process executes thread which creates threads and . is immediately taken by for execution as well as by such that becomes idle at time 1. However, and push on the thread stack such that at time 2 process starts execution of . In the following time steps, all processes now create the corresponding thread subtrees and, by waiting for the respective results, execute the corresponding code. At time 5, process is the quickest to grab and is the only one to continue execution while the other processes idle. In the end and take the newly created and .
Time Task Stack 0 1 2 3 4 5 6
All in all, the 15 lazy threads of the tree are executed by only six real threads of which only four threads are active at the same time (including the one thread which is blocked waiting for the result). Above figure illustrates this fact by a "bubble" for each active thread (with the different shadings representing the different processes).