Index 20 (250) abc.com.pl 9 wiedza i zycie3 Forsyth Diabelska alternatywa 955 (4) Chalker Jack L Zmierzch przy Studni Dusz Janusz A. Zajdel Limes Inferior (2) Brown Dan Kod Leonarda Da Vinci abc.com.pl 4 ip fragment.c |
[ Pobierz całość w formacie PDF ] .The difference is that in wait( ), the object lock is released and you can come out of the wait( ) because of a notify( ) as well as having the clock run out.The second form takes no arguments, and means that the wait( ) will continue until a notify( ) comes along and will not automatically terminate after a time.You can call wait( ) or notify( ) only for your own lock.Again, you can compile code that tries to use the wrong lock, but it will produce the same IllegalMonitorStateException message as before.You cant fool with someone elses lock, but you can ask another object to perform an operation that manipulates its own lock.So one approach is to create a synchronized method that calls notify( ) for its own object.However, in Notifier youll see the notify( ) call inside a synchronized block:synchronized(wn2) {wn2.notify();}where wn2 is the object of type WaitNotify2.This method, which is not part of WaitNotify2, acquires the lock on the wn2 object, at which point its legal for it to call notify( ) for wn2 and you wont get the IllegalMonitorStateException.wait( ) is typically used when youve gotten to the point where youre waiting for some other condition, under the control of forces outside your thread, to change and you dont want to idly wait by inside the thread.So wait( ) allows you to put the thread to sleep while waiting for the world to change, and only when a notify( ) or notifyAll( ) occurs does the thread wake up and check for changes.Thus, it provides a way to synchronize between threads.Blocking on I/OThe Sender puts data into the Writer and sleeps for a random amount of time.However, Receiver has no sleep( ), suspend( ), or wait( ).But when it does a read( ) it automatically blocks when there is no more data.class Sender extends Blockable { // sendprivate Writer out;public Sender(Container c, Writer out) {super(c);this.out = out;}public void run() {while(true) {for(char c = 'A'; c <= 'z'; c++) {try {i++;out.write(c);state.setText("Sender sent: "+ (char)c);sleep((int)(3000 * Math.random()));} catch(InterruptedException e) {System.err.println("Interrupted");} catch(IOException e) {System.err.println("IO problem");}}}}}class Receiver extends Blockable {private Reader in;public Receiver(Container c, Reader in) {super(c);this.in = in;}public void run() {try {while(true) {i++; // Show peeker it's alive// Blocks until characters are there:state.setText("Receiver read: "+ (char)in.read());}} catch(IOException e) {System.err.println("IO problem");}}}Both classes also put information into their state fields and change i so the Peeker can see that the thread is running.To set up a connection between the Sender and Receiver objects, a PipedWriter and PipedReader are created.Note that the PipedReader in must be connected to the PipedWriter out via a constructor argument.After that, anything thats placed in out can later be extracted from in, as if it passed through a pipe (hence the name).The in and out objects are then passed to the Receiver and Sender constructors, respectively, which treat them as Reader and Writer objects of any type (that is, they are upcast).DeadlockThere is no language support to help prevent deadlock; its up to you to avoid it by careful design.These are not comforting words to the person whos trying to debug a deadlocking program.The deprecation of stop( ),suspend( ), resume( ), and destroy( ) inJava 2There are times when a thread blockssuch as when it is waiting for inputand it cannot poll a flag as it does in Blocking.java.In these cases, you still shouldnt use stop( ), but instead you can use the interrupt( ) method in Thread to break out of the blocked code://: c14:Interrupt.java// The alternative approach to using// stop() when a thread is blocked.// <applet code=Interrupt width=200 height=100>// </applet>import javax.swing.*;import java.awt.*;import java.awt.event.*;import com.bruceeckel.swing.*;class Blocked extends Thread {public synchronized void run() {try {wait(); // Blocks} catch(InterruptedException e) {System.err.println("Interrupted");}System.out.println("Exiting run()");}}public class Interrupt extends JApplet {private JButtoninterrupt = new JButton("Interrupt");private Blocked blocked = new Blocked();public void init() {Container cp = getContentPane();cp.setLayout(new FlowLayout());cp.add(interrupt);interrupt.addActionListener(new ActionListener() {publicvoid actionPerformed(ActionEvent e) {System.out.println("Button pressed");if(blocked == null) return;Thread remove = blocked;blocked = null; // to release itremove.interrupt();}});blocked.start();}public static void main(String[] args) {Console.run(new Interrupt(), 200, 100);}} ///:~The wait( ) inside Blocked.run( ) produces the blocked thread.When you press the button, the blocked reference is set to null so the garbage collector will clean it up, and then the objects interrupt( ) method is called [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl. | |||||