Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: how to suspend and resume a running thread

Java: how to suspend and resume a running thread
{filelink=10866}

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 public class ThreadEtat extends JFrame implements Runnable
  	{
      private static final String[] listObjet =
          { "|", "/", "-", "", "|", "/", "-", "" };
       private Thread myThread;
       private JTextField mascotte;
       public ThreadEtat()
       	{
      mascotte = new JTextField();
      mascotte.setEditable(false);
      mascotte.setFont(new Font("Monospaced", Font.BOLD, 26));
      mascotte.setHorizontalAlignment(JTextField.CENTER);
      final JButton susprendre_anim = new JButton("suspend");
      final JButton reprendre_anim = new JButton("resume");
      susprendre_anim.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  suspendre();
              }
          });
      reprendre_anim.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  reprendre();
              }
          });
      JPanel pnl = new JPanel();
      pnl.setLayout(new GridLayout(0, 1, 3, 3));
      pnl.add(mascotte);
      pnl.add(susprendre_anim);
      pnl.add(reprendre_anim);
      setLayout(new FlowLayout(FlowLayout.CENTER));
      add(pnl);
      setTitle("Thread" Example");
      setSize(320, 200);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
  }
         // suspend method
       private void suspendre()
  {
      if ( myThread != null )
      {
          myThread.suspend();
      }
  }
   // resume method
       private void reprendre()
  {
      if ( myThread != null )
      	{
          myThread.resume();
      }
  }
       public void run() {
      try {
          myThread = Thread.currentThread();
          int count = 0;
          while ( true )
          	{
              mascotte.setText(
                  listObjet[ count % listObjet.length ]);
              Thread.sleep(200);
              count++;
          }
      } catch ( InterruptedException x ) {
      } finally {
          myThread = null;
      }
  }
       public static void main(String[] args)
       	{
      ThreadEtat obj = new ThreadEtat();
      Thread thr = new Thread(obj);
      thr.start();
  		}
   }

Users who found this page were searching for:

  • tkinter easy example
  • how to parse in a string from a dialog box in java
  • python show message
  • python howto program mouse
  • python db_conn execute sql server
  • perl ms access
  • java Polyline
  • java jlist multiple selection

No comments yet.

Leave a Comment