 // 一時停止と再開 [Thread3.java] 
 import java.applet.*; 
 import java.awt.*; 
 import java.awt.event.*;    // JDK 1.1 
 public class Thread3 extends Applet 
              implements ActionListener,Runnable{ // JDK 1.1 
    Button b_start,b_stop;   // ボタン 
    Thread th=null;          // スレッドオブジェクトの変数 
    int x,y;                 // 描画位置 
  public void init() {       // ロード時の初期化メソッド 
     x=20; y=40;             // x,y の初期値 
     b_start=new Button("START");  // STARTボタン 
     b_stop =new Button("STOP");   // STOPボタン 
     add(b_start);  add(b_stop);   // ボタンの追加 
     b_start.addActionListener(this);  // JDK 1.1 
     b_stop.addActionListener(this);   // JDK 1.1 
  } //end init 
  //============= JDK 1.1 イベント処理(ボタン) ============= 
  public void actionPerformed(ActionEvent e) { //イベント処理 
     if (e.getSource() == b_start) { 
        th.resume();                  //(a) スレッドの再開 
     } else if (e.getSource() == b_stop) { 
        th.suspend();                 //(b) スレッドの一時停止 
     } //end if 
  } //end actionPerformed 
     // +==== Thread1.java の後半部分と同じ  ====+ 
     // |   public void start() {               | 
     // |        :                              | 
     // |   public void run() {                 | 
     // |        :                              | 
     // |   public void stop() {                | 
     // |        :                              | 
     // |   public void paint(Graphics g) {     | 
     // |        :                              | 
     // +=======================================+ 
    public void start() { 
       if (th == null) {        //(d) 
          th=new Thread(this);  //(d) スレッドの作成 
          th.start();           //(d) スレッドの開始 
       } 
    } //end start 
    public void run() { 
       while( true ) {               //(e) ループ 
          try {                      //(f) 例外処理 
             x+=20;                  //(g) x位置を増やす 
             if (x >= 200) { x=20; } //(g) x位置を戻す 
             repaint();              //(h) 作画 
             th.sleep(1000);    //(i) 停止する時間(ミリ秒) 
          } catch(InterruptedException e) { } // 例外処理 
       } //end while 
    } //end run 
    public void stop() { 
       if (th != null) { 
          th.stop();  // スレッドの停止 
          th=null; 
       } 
    } //end stop 
    //=================== paintメソッド ==================== 
    public void paint(Graphics g) { 
       g.setColor(Color.blue); 
       g.fillOval(x,y,50,50);      // 円を描く 
    } //end paint 
 } //end Thread3 
