HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Persistent Threads: Part 2"
Volume: 4 Issue: 2, p. 8

	

Listing 1: The PersThread Application

// PersThread.java
... 

public class PersThread extends Frame 
                        implements Runnable {
    final static String SER_FILE_NAME = ...
    final static int MAX_COUNTER = 300;
    static Thread thread = null;
    private int threadStage = 0;
    private int counter = 0;
    private PTCanvas canvas;

    private PersThread() {
        // Builds the user interface
        ...
    }

    synchronized int getCounter() {
        return counter;
    }

    void computing(int limit) {
        boolean mustBeInterrupted = false;
        while (limit > 0) {
            synchronized (this) {
                if (counter < MAX_COUNTER)
                    counter++;
            }
            canvas.repaint();
            limit--;
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
                mustBeInterrupted = true;
            }
        }
        if (mustBeInterrupted)
            Thread.currentThread().interrupt();
    }

    void breakPoint() {
        if (Thread.interrupted()) {
            serialize();
            System.exit(0);
        }
    }

    // The run() method of the persistent thread
    public void run() {
        if (threadStage == 0) {
            computing(100);
            threadStage = 1;
        }
        breakPoint();
        if (threadStage == 1) {
            computing(100);
            threadStage = 2;
        }
        breakPoint();
        if (threadStage == 2) {
            computing(100);
            threadStage = 3;
        }
    }

    public void serialize() {
        // Tries to serialize the thread's state
        ...
    }

    public static void main(String args[]) {
        // Tries to deserialize the thread's state
        // If deserialization fails then
        // a new PersThread instance will be created
        ...
    }

// Inner class
class PTCanvas extends Canvas {
    ...

    public void paint(Graphics g) {
        // Makes a copy of the counter member var.
        int counter = getCounter();
        // Paints the canvas
        ...
    }
}

// Inner class
class PTAdapter extends WindowAdapter 
    implements ActionListener, Serializable {

    public void actionPerformed(ActionEvent e) {
        appExit();
    }

    public void windowClosing(WindowEvent e) {
        appExit();
    }

    void appExit() {
        if (thread != null && thread.isAlive() 
            && getCounter() < MAX_COUNTER)
            thread.interrupt();
        else {
            // Deletes the .ser file
            ...
            System.exit(0);
        }
    }
}

}

  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.