ThreadMentor: Visualizing Linear Array Sort

Compile and run this program with input 4, 3, 2 and 1. Run this program for a while and you may see a History Graph window like the following one. It shows that four threads have been created so far: Master, Sort1, Sort2 and Sort3. The last three threads are sorting threads. On the history bar of Master, we see five CS tags, indicating all four messages have been sent followed by the END_OF_DATA message. Since all channels are asynchronous, Master exits even before all of its messages are received. In fact, it exits before the third message 2 is received by thread Sort1.

Let us examine Sort1. It receives the first two messages (i.e., 4 and 3) from thread Master. Thread Sort1 memorizes 4. When 3 is received, since 3 is less than 4 and since Sort1 is the last thread in chain, Sort1 creates Sort2 and sends 4 to Sort2. Now, thread Sort2 is created, receives 4 and memorizes it. Then, Sort1 receives the third message (i.e., 2) from Master. Since 2 is less than the number Sort1 memorizes (i.e., 3), Sort1 memorizes 2 and sends 3 to Sort2. This is shown by the second CS tag on Sort1's history bar. Then, Sort1 receives the fourth message (i.e., 1) from Master. Because 1 is smaller than 2, Sort1 sends 2 to thread Sort2 and memorizes 1. Then, thread Sort1 receives the END_OF_DATA message. This is indicated as the last CR tag.

As for Sort2, after receives 3 from thread Sort1 as indicated by the second CR tag, because 3 is smaller 4, the number that Sort2 memorizes, 3 must be sent out. However, since thread Sort2 is the last in chain, it creates Sort3 and sends 4 to it. This is indicated by the first CS tag on thread Sort2's history bar. After 4 is sent, thread Sort2 receives message 2 from thread Sort1 and this is the last CR tag on Sort2's history bar. Since all channels are asynchronous, message 4 sent by thread Sort2 to thread Sort3 has not been received.

Since the UserDefinedThreadIDs for Master, Sort1, Sort2 and Sort3 are 0, 1, 2 and 3, respectively, the channels from Master to thread Sort1, from thread Sort1 to thread Sort2, and from thread Sort2 to Sort3 are Channel0-1, Channel1-2 and Channel2-3. Therefore, at the last moment of the screen shot, we have the above three channels created. This can be seen with the main window:

From this main widnow, we learn that channels Channel0-1 and Channel1-2 have status Received because the last messages sent to them have been received (i.e., send-receive links). However, the status of channel Channel2-3 is Sending because the message sent by thread Sort2 to thread Sort3 has not been received (i.e., the dangling CS tag on thread Sort2's history bar).

Click on the three channel names to bring up their windows as shown below. As you can see, thread Sort1 has received messages 4, 3, 2, 1 and -1 (i.e., END_OF_DATA) from channel Channel0-1, and thread Sort2 as received messages 4, 3 and 2 from channel Channel1-2. Channel2-3 has a message 4 that has not been received by thread Sort3 which is just created. Therefore, our observations are well justified.

Channel0-1
Channel1-2
Channel2-3