public class RunnableObject implements Runnable
1: // Fig. 23.17: RunnableObject.java
2: // Runnable that writes a random character to a JLabel
3: import java.util.Random;
4: import java.util.concurrent.locks.Condition;
5: import java.util.concurrent.locks.Lock;
6: import javax.swing.JLabel;
7: import javax.swing.SwingUtilities;
8: import java.awt.Color;
9:
10: public class RunnableObject implements Runnable
11: {
12: private static Random generator = new Random(); // for random letters
13: private Lock lockObject; // application lock; passed in to constructor
14: private Condition suspend; // used to suspend and resume thread
15: private boolean suspended = false; // true if thread suspended
16: private JLabel output; // JLabel for output
17:
18: public RunnableObject( Lock theLock, JLabel label )
19: {
20: lockObject = theLock; // store the Lock for the application
21: suspend = lockObject.newCondition(); // create new Condition
22: output = label; // store JLabel for outputting character
23: } // end RunnableObject constructor
24:
25: // place random characters in GUI
26: public void run()
27: {
28: // get name of executing thread
29: final String threadName = Thread.currentThread().getName();
30:
31: while( true ) // infinite loop; will be terminated from outside
32: {
33: try
34: {
35: // sleep for up to 1 second
36: Thread.sleep( generator.nextInt( 1000 ) );
37:
38: lockObject.lock(); // obtain the lock
39: try
40: {
41: while ( suspended ) // loop until not suspended
42: {
43: suspend.await(); // suspend thread execution
44: } // end while
45: } // end try
46: finally
47: {
48: lockObject.unlock(); // unlock the lock
49: } // end finally
50: } // end try
51: // if thread interrupted during wait/sleep
52: catch ( InterruptedException exception )
53: {
54: exception.printStackTrace(); // print stack trace
55: } // end catch
56:
57: // display character on corresponding JLabel
58: SwingUtilities.invokeLater(
59: new Runnable()
60: {
61: // pick random character and display it
62: public void run()
63: {
64: // select random uppercase letter
65: char displayChar =
66: ( char ) ( generator.nextInt( 26 ) + 65 );
67:
68: // output character in JLabel
69: output.setText( threadName + ": " + displayChar );
70: } // end method run
71: } // end inner class
72: ); // end call to SwingUtilities.invokeLater
73: } // end while
74: } // end method run
75:
76: // change the suspended/running state
77: public void toggle()
78: {
79: suspended = !suspended; // toggle boolean controlling state
80:
81: // change label color on suspend/resume
82: output.setBackground( suspended ? Color.RED : Color.GREEN );
83:
84: lockObject.lock(); // obtain lock
85: try
86: {
87: if ( !suspended ) // if thread resumed
88: {
89: suspend.signal(); // resume thread
90: } // end if
91: } // end try
92: finally
93: {
94: lockObject.unlock(); // release lock
95: } // end finally
96: } // end method toggle
97: } // end class RunnableObject
98:
99: /**************************************************************************
100: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
101: * Pearson Education, Inc. All Rights Reserved. *
102: * *
103: * DISCLAIMER: The authors and publisher of this book have used their *
104: * best efforts in preparing the book. These efforts include the *
105: * development, research, and testing of the theories and programs *
106: * to determine their effectiveness. The authors and publisher make *
107: * no warranty of any kind, expressed or implied, with regard to these *
108: * programs or to the documentation contained in these books. The authors *
109: * and publisher shall not be liable in any event for incidental or *
110: * consequential damages in connection with, or arising out of, the *
111: * furnishing, performance, or use of these programs. *
112: *************************************************************************/