ThreadMentor: Visualizing Array Merge

In terms of visualization, this program is quite simple. Every child thread is created, does a simple thing, and terminates. Only the main does a thread join. Therefore, we should not anticipate very much activities from this example.

Suppose we run this program with two sorted arrays, each of which has four elements. Run the program for a very short amount of time, we will see a Thread Status window similar to the following. Please keep in mind that you might not get a window exactly like the following because the execution behavior of a threaded program is dynamic that depends on may factors. As shown in the figure, the main program has created five threads, MergeA-0, MergeA-1, MergeA-2, MergeA-3 and MergeB-0. The first four are created to find the positions of array elements a[0], a[1], a[2] and a[3] in the merged array, and the fifth is created to find the position of b[0] in the merged array. The main thread is still running because there are three more threads to be created. Since each thread only does some simple task, three threads have terminated (i.e., MergeA-0, MergeA-1 and MergeA-2).

In the meantime, the History Graph windows shows the following. If you are careful, you should see that the thread names are the same as those in the Thread Status window. However, there are some minor differences. Please compare the content of these windows before continue.

What are the differences?

  1. Some threads have no history bar
    Why? This is because the time interval between creation and termination is too short so that the length of the history bar is shorter than the minimal resolution that is being used by the History Graph window. We shall return to this point on a later page.
  2. Why are the status of some threads are different in these windows?
    Check thread MergeB-0 in both windows. This thread is running in the Thread Status window but has no history bar in the History Graph window. This is because the visualization system updates the windows one-by-one. If you see this discrepancy, do not worry, and the window will be updated soon. This usually happens when the program is run on a slow machine or the user slows down the execution pace.

The following shows the Thread Status window near the end of execution. As you can see, all eight child threads have terminated and the main thread is joining.

The following History Graph window shows that the main thread has joined with four child threads because there are four JN tags shown.

Click on one of these four JN tags brings up the source window with the thread join statement highlighted.