01: import java.util.*;
02: 
03: /**
04:    This runnable executes a sort algorithm.
05:    When two elements are compared, the algorithm
06:    pauses and updates a panel.
07: */
08: public class Sorter implements Runnable
09: {
10:    public Sorter(Object[] values, ArrayPanel panel,
11:       Gate gate)
12:    {
13:       this.values = values;
14:       this.panel = panel;
15:       this.gate = gate;
16:    }
17: 
18:    public void run()
19:    {
20:       Comparator comp = new
21:          Comparator()
22:          {
23:             public int compare(Object o1, Object o2)
24:             {
25:                panel.setValues(values, o1, o2);
26:                try
27:                {
28:                   if (gate.isActive())
29:                      gate.waitForOpen();
30:                   else
31:                      Thread.sleep(DELAY);
32:                }
33:                catch (InterruptedException exception)
34:                {
35:                   Thread.currentThread().interrupt();
36:                }
37:                return ((Integer) o1).compareTo(o2);
38:             }
39:          };
40:       MergeSorter.sort(values, comp);
41:       panel.setValues(values, null, null);
42:    }
43: 
44:    private Object[] values;
45:    private ArrayPanel panel;
46:    private Gate gate;
47:    private static final int DELAY = 100;
48: }