Package: java.applet.* |
Product: JDK |
Release: 1.1.x |
Related Links: |
General
General
General
|
Comment: |
Listing 1: BaseFriendlyApplet. import java.awt.*; import java.awt.event.*; public abstract class BaseFriendlyApplet extends java.applet.Applet implements ActionListener { static final int MAX_COUNTER= Integer.MAX_VALUE; static final int BIG_NUMBER = 100000000; static final int MAX_SUBCOUNTER = BIG_NUMBER*10; int counter = 0; int subcounter = 0; Label label; public void init() { setLayout(new BorderLayout()); label = new Label(“0”, Label.CENTER); add(label, BorderLayout.CENTER); Panel panel = new Panel(); panel.setLayout(new GridLayout(1, 2)); Button button_s = new Button(“Suspend”); button_s.addActionListener(this); panel.add(button_s); Button button_r = new Button(“Resume”); button_r.addActionListener(this); panel.add(button_r); add(panel, BorderLayout.SOUTH); } void nextFrame() { for (int i = 0; i < BIG_NUMBER; i++) if (subcounter < MAX_SUBCOUNTER) subcounter++; else subcounter = 1; if (counter < MAX_COUNTER) counter++; else counter = 0; label.setText(Integer.toString(counter)); } public void actionPerformed(ActionEvent e) { String what = e.getActionCommand(); if (what.equals(“Suspend”)) stop(); if (what.equals(“Resume”)) start(); } } Listing 2: BadFriendlyApplet. public class BadFriendlyApplet extends BaseFriendlyApplet implements Runnable { Thread thread = null; boolean threadSuspended = false; public synchronized void start() { if (thread == null) { thread = new Thread(this); thread.start(); } else if (threadSuspended) { threadSuspended = false; thread.resume(); } } public synchronized void stop() { if (thread != null && !threadSuspended) { threadSuspended = true; thread.suspend(); } } public synchronized void destroy() { thread.stop(); thread = null; } public void run() { while (true) { nextFrame(); try { Thread.sleep(10); } catch (InterruptedException e) { } } } } Listing 3: GoodFriendlyApplet. public class GoodFriendlyApplet extends BaseFriendlyApplet implements Runnable { Thread thread = null; boolean threadSuspended = false; public synchronized void start() { if (thread == null) { thread = new Thread(this); thread.start(); } else if (threadSuspended) { threadSuspended = false; notifyAll(); } } public synchronized void stop() { if (thread != null && !threadSuspended) threadSuspended = true; } public synchronized void destroy() { thread.interrupt(); thread = null; } public void run() { while (true) { nextFrame(); try { Thread.sleep(10); if (threadSuspended) synchronized (this) { while (threadSuspended) wait(); } } catch (InterruptedException e) { break; } } } } Listing 4: BetterFriendlyApplet. public class BetterFriendlyApplet extends BaseFriendlyApplet implements Runnable { Thread thread = null; boolean threadSuspended = false; private final Object LOCK = new Object(); public void start() { synchronized (LOCK) { if (thread == null) { thread = new Thread(this); thread.start(); } else if (threadSuspended) { threadSuspended = false; LOCK.notify(); } } } public void stop() { synchronized (LOCK) { if (thread != null && !threadSuspended) threadSuspended = true; } } public void destroy() { synchronized (LOCK) { thread.interrupt(); thread = null; } } public void run() { while (true) { nextFrame(); try { Thread.sleep(10); if (threadSuspended) synchronized (LOCK) { if (threadSuspended) LOCK.wait(); } } catch (InterruptedException e) { break; } } } } Listing 5: BestFriendlyApplet. public class BestFriendlyApplet extends BaseFriendlyApplet implements Runnable { Thread thread = null; boolean intrCanceled = false; private final Object LOCK = new java.io.Serializable() {}; public void start() { synchronized (LOCK) { if (thread == null) { thread = new Thread(this); thread.start(); } else if (thread.isInterrupted() && !intrCanceled) intrCanceled = true; } } public void stop() { synchronized (LOCK) { if (thread != null) if (!thread.isInterrupted()) { intrCanceled = false; thread.interrupt(); } else if (intrCanceled) intrCanceled = false; } } public void run() { while (true) { nextFrame(); try { Thread.sleep(10); } catch (InterruptedException e) { synchronized (LOCK) { if (!intrCanceled) { thread = null; break; } else intrCanceled = false; } } } } } |