注意这个新的线程一定要在开始事件中创建,例如:
Thread thread1=new Thread();
这句一定放在开始事件中

解决方案 »

  1.   

    “thread1.stop();  //我现在知道这样调用肯定是错误的”1.关键问题:线程只能被执行一次。如果想执行多次,必须:
    把线程的声明与定义放到“开始”按钮的事件处理程序内。2.线程的停止通过判断某个标记来做结束。
      

  2.   


     如果Thread thread1 = new Thread()放在“开始”按钮的事件中的话,
     那么每次点击“开始”按钮,将会生成一个新的线程
     
      

  3.   

    样例代码:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class TestMuchRun
        extends javax.swing.JFrame
    {
        private boolean isRun;
        FlowLayout flowLayout1 = new FlowLayout();
        JButton ButtonStart = new JButton();
        JButton ButtonEnd = new JButton();
        class Test
            implements Runnable
        {
            public void run()
            {
                while (isRun)
                {
                    System.out.println("I am running!!!!");
                    Thread.currentThread().yield();
                }
                System.out.println("I am going!!!bye bye!!!!!!!");
            }
        }    public TestMuchRun()
        {
            try
            {
                jbInit();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }    private void jbInit() throws Exception
        {
            this.setBounds(0,0,500,500);
            ButtonStart.setText("Start");
            ButtonStart.addMouseListener(new TestMuchRun_ButtonStart_mouseAdapter(this));
            this.getContentPane().setLayout(flowLayout1);
            ButtonEnd.setVerifyInputWhenFocusTarget(true);
            ButtonEnd.setText("End");
            ButtonEnd.addMouseListener(new TestMuchRun_ButtonEnd_mouseAdapter(this));
            this.getContentPane().add(ButtonStart, null);
            this.getContentPane().add(ButtonEnd, null);
        }    void ButtonStart_mouseClicked(MouseEvent e)
        {
            isRun = true;
            new Thread(new Test()).start();
        }    void ButtonEnd_mouseClicked(MouseEvent e)
        {
            isRun = false;
        }    public static void main(String[] args)
        {
            TestMuchRun t = new TestMuchRun();
            t.setVisible(true);
        }
    }
    class TestMuchRun_ButtonStart_mouseAdapter
        extends java.awt.event.MouseAdapter
    {
        TestMuchRun adaptee;    TestMuchRun_ButtonStart_mouseAdapter(TestMuchRun adaptee)
        {
            this.adaptee = adaptee;
        }    public void mouseClicked(MouseEvent e)
        {
            adaptee.ButtonStart_mouseClicked(e);
        }
    }class TestMuchRun_ButtonEnd_mouseAdapter
        extends java.awt.event.MouseAdapter
    {
        TestMuchRun adaptee;    TestMuchRun_ButtonEnd_mouseAdapter(TestMuchRun adaptee)
        {
            this.adaptee = adaptee;
        }    public void mouseClicked(MouseEvent e)
        {
            adaptee.ButtonEnd_mouseClicked(e);
        }
    }
      

  4.   

    你調用stop方法﹐線程不是暫停﹐而是終止了線程﹐下次你要再啟動線程的話﹐你需要重新生成線程的實例
      Thread thread1 = new Thread()
      thread1.start();
    如你所說﹐用stop方法不安全﹐可用如下方法代替
    class YourThread extends Thread{
      private boolean stopThread;
      public void requestStop(){
        stopThread = true;
      }
      public void run(){
        while(!stopThread){
           ...
        }
      }
    }要停止時調用線程的requestStop方法如果你不是要中止線程﹐而是要使其暫停的話﹐可以調用線程的wait()方法﹐喚醒線程用notifyAll()方法
      

  5.   

    已经终止的线程是不能重新开始的。只能重新创建线程对象,来开始一个新的线程。
    你可以注意对线程状态的描述,线程终止后其状态叫做dead死人又怎么能复活呢?:)
      

  6.   


      谢谢大家,特别感谢feiyuegaoshan(飞跃) !