public class RandomCharacters extends JFrame implements ActionListener
1: // Fig. 23.18: RandomCharacters.java
2: // Class RandomCharacters demonstrates the Runnable interface
3: import java.awt.Color;
4: import java.awt.GridLayout;
5: import java.awt.event.ActionEvent;
6: import java.awt.event.ActionListener;
7: import java.util.concurrent.Executors;
8: import java.util.concurrent.ExecutorService;
9: import java.util.concurrent.locks.Condition;
10: import java.util.concurrent.locks.Lock;
11: import java.util.concurrent.locks.ReentrantLock;
12: import javax.swing.JCheckBox;
13: import javax.swing.JFrame;
14: import javax.swing.JLabel;
15:
16: public class RandomCharacters extends JFrame implements ActionListener
17: {
18: private final static int SIZE = 3; // number of threads
19: private JCheckBox checkboxes[]; // array of JCheckBoxes
20: private Lock lockObject = new ReentrantLock( true ); // single lock
21:
22: // array of RunnableObjects to display random characters
23: private RunnableObject[] randomCharacters =
24: new RunnableObject[ SIZE ];
25:
26: // set up GUI and arrays
27: public RandomCharacters()
28: {
29: checkboxes = new JCheckBox[ SIZE ]; // allocate space for array
30: setLayout( new GridLayout( SIZE, 2, 5, 5 ) ); // set layout
31:
32: // create new thread pool with SIZE threads
33: ExecutorService runner = Executors.newFixedThreadPool( SIZE );
34:
35: // loop SIZE times
36: for ( int count = 0; count < SIZE; count++ )
37: {
38: JLabel outputJLabel = new JLabel(); // create JLabel
39: outputJLabel.setBackground( Color.GREEN ); // set color
40: outputJLabel.setOpaque( true ); // set JLabel to be opaque
41: add( outputJLabel ); // add JLabel to JFrame
42:
43: // create JCheckBox to control suspend/resume state
44: checkboxes[ count ] = new JCheckBox( "Suspended" );
45:
46: // add listener which executes when JCheckBox is clicked
47: checkboxes[ count ].addActionListener( this );
48: add( checkboxes[ count ] ); // add JCheckBox to JFrame
49:
50: // create a new RunnableObject
51: randomCharacters[ count ] =
52: new RunnableObject( lockObject, outputJLabel );
53:
54: // execute RunnableObject
55: runner.execute( randomCharacters[ count ] );
56: } // end for
57:
58: setSize( 275, 90 ); // set size of window
59: setVisible( true ); // show window
60:
61: runner.shutdown(); // shutdown runner when threads finish
62: } // end RandomCharacters constructor
63:
64: // handle JCheckBox events
65: public void actionPerformed( ActionEvent event )
66: {
67: // loop over all JCheckBoxes in array
68: for ( int count = 0; count < checkboxes.length; count++ )
69: {
70: // check if this JCheckBox was source of event
71: if ( event.getSource() == checkboxes[ count ] )
72: randomCharacters[ count ].toggle(); // toggle state
73: } // end for
74: } // end method actionPerformed
75:
76: public static void main( String args[] )
77: {
78: // create new RandomCharacters object
79: RandomCharacters application = new RandomCharacters();
80:
81: // set application to end when window is closed
82: application.setDefaultCloseOperation( EXIT_ON_CLOSE );
83: } // end main
84: } // end class RandomCharacters
85:
86:
87: /**************************************************************************
88: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
89: * Pearson Education, Inc. All Rights Reserved. *
90: * *
91: * DISCLAIMER: The authors and publisher of this book have used their *
92: * best efforts in preparing the book. These efforts include the *
93: * development, research, and testing of the theories and programs *
94: * to determine their effectiveness. The authors and publisher make *
95: * no warranty of any kind, expressed or implied, with regard to these *
96: * programs or to the documentation contained in these books. The authors *
97: * and publisher shall not be liable in any event for incidental or *
98: * consequential damages in connection with, or arising out of, the *
99: * furnishing, performance, or use of these programs. *
100: *************************************************************************/