We give an overview of the components and the execution flow that we will use in the solution. There will be two active components. The first active component will be the monitoring thread that will poll the memory usage. The second active component is the Swing event processing thread.
Picture 2: Thread Interaction Memory Monitor
The above diagram shows the parallel flow between the two active components. The two components communicate over the event queue. The frame thread is the event producer whereas the Swing thread is basically the event consumer. The Swing thread might also produce new events. In summary the parallel flow will work as follows:
Main Scenario Frame Thread:
1. The frame thread calculates the actual memory usage.
2. The frame thread posts an event with a new value for the memory panel.
3. The frame thread sleeps a particular amount of time.
4. The flow continues with step 1.
Main Scenario Swing Thread:
1. The swing thread receives a value event.
2. The swing thread requests the memory panel to add a new value.
3. The memory panel adds a new value.
4. The memory panel posts a repaint event.
5. The flow continues with step 1.
Alternative Scenario Swing Thread:
1a.1. The swing thread receives a repaint event.
1a.2. The swing thread requests the memory panel to paint itself.
1a.3. The memory panel draws itself.
1a.4. The flow continues with step 1.
For the alternative scenario of the Swing thread there will be not much coding on our side. We will use an out of the box component from JFreeChart. This component does all the drawing for us. The component does also necessary computations for us, such as determining min and max values and computing scale ticks.
The main scenario of the Swing thread does only require a little coding. The Swing queue and the Swing thread are already part of the Swing framework delivered with the Java runtime environment. But what will need to implement is a method to incorporate the data into the chart, since the chart does not know how we would like the data be placed.
Finally we will need to implement the main scenario of the frame thread. Similarly to the Swing thread it is basically a forever loop. We do not show here is how the thread is stopped by the memory frame. Also there is no depiction of the creation of the memory frame. The full source code can be found in the appendix. Certain aspects will be explained in the subsequent sections.