Web design software - Chapter 15 Multithreading 873 87 // make all
Chapter 15 Multithreading 873 87 // make all waiting threads ready to execute, so they 88 // can terminate themselves 89 notifyAll(); 90 } 91 92 // handle button events 93 public synchronized void actionPerformed( ActionEvent event ) 94 { 95 for ( int count = 0; count < checkboxes.length; count++ ) { 96 97 if ( event.getSource() == checkboxes[ count ] ) { 98 suspended[ count ] = !suspended[ count ]; 99 100 // change label color on suspend/resume 101 outputs[ count ].setBackground( 102 !suspended[ count ] ? Color.green : Color.red ); 103 104 // if thread resumed, make sure it starts executing 105 if ( !suspended[ count ] ) 106 notifyAll(); 107 108 return; 109 } 110 } 111 } 112 113 // private inner class that implements Runnable so objects 114 // of this class can control threads 115 private class RunnableObject implements Runnable { 116 117 // Place random characters in GUI. Local variables 118 // currentThread and index are declared final so 119 // they can be used in an anonymous inner class. 120 public void run() 121 { 122 // get reference to executing thread 123 final Thread currentThread = Thread.currentThread(); 124 125 // determine thread's position in array 126 final int index = getIndex( currentThread ); 127 128 // loop condition determines when thread should stop 129 while ( threads[ index ] == currentThread ) { 130 131 // sleep from 0 to 1 second 132 try { 133 Thread.sleep( ( int ) ( Math.random() * 1000 ) ); 134 135 // Determine whether thread should suspend 136 // execution. Use applet as monitor. 137 synchronized( RandomCharacters.this ) { 138 Fig. 15.17 Demonstrating the Runnableinterface, suspending threads and resuming threads (part 3 of 5).
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.