Visual suspend and resume : Thread Status « Threads « Java






Visual suspend and resume

Visual suspend and resume
 

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SwingSuspendResume extends JPanel implements Runnable {

  private static final String[] symbolList = { "|", "/", "-", "\\", "|", "/",
      "-", "\\" };

  private Thread runThread;

  private JTextField symbolTF;

  public SwingSuspendResume() {
    symbolTF = new JTextField();
    symbolTF.setEditable(false);
    symbolTF.setFont(new Font("Monospaced", Font.BOLD, 26));
    symbolTF.setHorizontalAlignment(JTextField.CENTER);

    final JButton suspendB = new JButton("Suspend");
    final JButton resumeB = new JButton("Resume");

    suspendB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        suspendNow();
      }
    });

    resumeB.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        resumeNow();
      }
    });

    JPanel innerStackP = new JPanel();
    innerStackP.setLayout(new GridLayout(0, 1, 3, 3));
    innerStackP.add(symbolTF);
    innerStackP.add(suspendB);
    innerStackP.add(resumeB);

    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.add(innerStackP);
  }

  private void suspendNow() {
    if (runThread != null) { // avoid NullPointerException
      runThread.suspend();
    }
  }

  private void resumeNow() {
    if (runThread != null) { // avoid NullPointerException
      runThread.resume();
    }
  }

  public void run() {
    try {
      // Store this for the suspendNow() and
      // resumeNow() methods to use.
      runThread = Thread.currentThread();
      int count = 0;

      while (true) {
        // each time through, show the next symbol
        symbolTF.setText(symbolList[count % symbolList.length]);
        Thread.sleep(200);
        count++;
      }
    } catch (InterruptedException x) {
      // ignore
    } finally {
      // make sure that the reference to it is also lost.
      runThread = null;
    }
  }

  public static void main(String[] args) {
    SwingSuspendResume vsr = new SwingSuspendResume();
    Thread t = new Thread(vsr);
    t.start();

    JFrame f = new JFrame();
    f.setContentPane(vsr);
    f.setSize(320, 200);
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
}

           
         
  








Related examples in the same category

1.Is thread aliveIs thread alive
2.Thread sleepThread sleep
3.Another way to stop a threadAnother way to stop a thread
4.Another way to suspend and resumeAnother way to suspend and resume
5.Thread sleep and interruptThread sleep and interrupt
6.Daemon ThreadDaemon Thread
7.Pausing the Current Thread: a thread can temporarily stop execution.
8.Pausing a Thread: set a variable that the thread checks occasionally, call Object.wait()
9.set Uncaught Exception Handler
10.Monitor a thread's status.
11.Pause the execution
12.Interrupt a thread.
13.Stopping a Thread: set a variable that the thread checks occasionally
14.Determining When a Thread Has Finished
15.Add a delay
16.Pause the execution of a thread using sleep()