[ Pobierz całość w formacie PDF ]
.There’s also a button andactionPerformed( ) clause to stop all of the Peeker objects,which provides a demonstration of the alternative to the deprecated (in Java 2)stop( ) method of Thread.[ Add Comment ]To set up a connection between theSender and Receiver objects, a PipedWriter andPipedReader are created.Note that the PipedReader in mustbe connected to the PipedWriter out via a constructor argument.After that, anything that’s placed in out can later be extractedfrom in, as if it passed through a pipe (hence the name).The inand out objects are then passed to the Receiver and Senderconstructors, respectively, which treat them as Reader and Writerobjects of any type (that is, they are upcast).[ Add Comment ]The array of Blockable referencesb is not initialized at its point of definition because the piped streamscannot be set up before that definition takes place (the need for the tryblock prevents this).///:Continuing/////////// Testing Everything ///////////public class Blocking extends JApplet {private JButtonstart = new JButton("Start"),stopPeekers = new JButton("Stop Peekers");private boolean started = false;private Blockable[] b;private PipedWriter out;private PipedReader in;class StartL implements ActionListener {public void actionPerformed(ActionEvent e) {if(!started) {started = true;for(int i = 0; i < b.length; i++)b[i].start();}}}class StopPeekersL implements ActionListener {public void actionPerformed(ActionEvent e) {// Demonstration of the preferred// alternative to Thread.stop():for(int i = 0; i < b.length; i++)b[i].stopPeeker();}}public void init() {Container cp = getContentPane();cp.setLayout(new FlowLayout());out = new PipedWriter();try {in = new PipedReader(out);} catch(IOException e) {System.err.println("PipedReader problem");}b = new Blockable[] {new Sleeper1(cp),new Sleeper2(cp),new SuspendResume1(cp),new SuspendResume2(cp),new WaitNotify1(cp),new WaitNotify2(cp),new Sender(cp, out),new Receiver(cp, in)};start.addActionListener(new StartL());cp.add(start);stopPeekers.addActionListener(new StopPeekersL());cp.add(stopPeekers);}public static void main(String[] args) {Console.run(new Blocking(), 350, 550);}} ///:~In init( ), notice the loopthat moves through the entire array and adds the state andpeeker.status text fields to the page.[ Add Comment ]When the Blockable threads areinitially created, each one automatically creates and starts its ownPeeker.So you’ll see the Peekers running before theBlockable threads are started.This is important, as some of thePeekers will get blocked and stop when the Blockable threadsstart, and it’s essential to see this to understand that particular aspectof blocking.[ Add Comment ]DeadlockBecause threads can become blockedand because objects can have synchronized methods that preventthreads from accessing that object until the synchronization lock is released,it’s possible for one thread to get stuck waiting for another thread,which in turn waits for another thread, etc., until the chain leads back to athread waiting on the first one.You get a continuous loop of threads waiting oneach other and no one can move.This is called deadlock.The claim isthat it doesn’t happen that often, but when it happens to you it’sfrustrating to debug.[ Add Comment ]There is no language support to helpprevent deadlock; it’s up to you to avoid it by careful design.These arenot comforting words to the person who’s trying to debug a deadlockingprogram
[ Pobierz całość w formacie PDF ]