Anonymous web server - Chapter 15 Multithreading 875 Fig. 15.17 Demonstrating the
Chapter 15 Multithreading 875 Fig. 15.17 Demonstrating the Runnableinterface, suspending threads and resuming threads (part 5 of 5). The whileloop at lines 129 179 continues to execute as long as the specified Thread reference is equal to the reference to the currently executing thread (currentThread). In each iteration of the loop, the thread sleeps for a random interval from 0 to 1 second. When the user clicks the JCheckBoxto the right of a particular JLabel, the corresponding Thread should be suspended (temporarily prevented from executing) or resumed (allowed to continue executing). In previous versions of Java, methods suspend and resumeof class Threadwere provided to suspend and resume a thread s execution. These methods are now deprecated (i.e., they should no longer be used) because they introduce the possibility of deadlock in a program if they are not used correctly. Suspending and resuming of a thread can be implemented using thread synchronization and methods wait and notify of class Object. Lines 137 147 define a synchronized block of code (also called a synchronized statement) that helps suspend the currently executing Thread. When the Threadreaches the synchronizedblock, the applet object (referenced with RandomCharacters.this) is locked and the while structure tests suspended[index]to determine if the Threadshould be suspended (i.e., true). If so, line 144 invokes method waiton the applet object to place the Threadin the waiting state. [Note the use of RandomCharacters.this to access the applet class s this reference from the private inner class RunnableObject.] When the Thread should resume, the program tells all waiting threads to become ready to execute (we will discuss this shortly). However, only the resumed thread will get a chance to execute. The other suspended thread(s) will reenter the waiting state. Lines 156 177 use SwingUtilities method invokeLater to update the JLabel for the appropriate thread. This example uses an anonymous inner class to implement the Runnableinterface (lines 161 175) and passes the anonymous inner class object to invokeLater. Lines 167 168 choose a random character from the alphabetstring. Lines 170 172 display the character on the appropriate JLabelobject. Software Engineering Observation 15.4 An inner class can reference its outer class s this reference by preceding the this reference with the outer class name and a dot operator. If the user clicks the Suspended check box next to a particular JLabel, the program invokes method actionPerformed (lines 93 111). The method determines which checkbox received the event. Using the index of that checkbox in array outputs, line 98 toggles the corresponding boolean in array suspended. Lines 101 102 set the background color of the JLabelto red if the thread is being suspended and green if the thread
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.