public class Test implements java.lang.Runnable{
 private Thread t ;
 public Text(){
     t = new Thread(this);
 }
}现在我有一个按钮button_start.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
t.start();
   }
});第一次按的时候线程可以启动,第二次按的时候出异常了,怎样让他每按一下按钮都能正常启动线程呢

解决方案 »

  1.   

    public class Test implements java.lang.Runnable{
     // private Thread t ;
     public Text(){
         new Thread(this);
     }
    }
    t.start();  //改为 new Text().start();
      

  2.   

    public class Test implements java.lang.Runnable{
     private static Array<Thread> t ;
     private static count=0;
     public Text(){
         t.add(new Thread(this));
         count++;
     }
    }button_start.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            t.get(count).start();
           }
        });
      

  3.   

    可以在下次点击按钮时判断thread t是否已经启动,如果已经启动则直接返回。
      

  4.   

    public class Test implements java.lang.Runnable{
     Thread t ;  // 去掉那个 private
     public Text(){
         t = new Thread(this);
     }
    }
    button_start.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            new Text().t.start(); // 每次重新生成一个
           }
        });
      

  5.   


    用isAilve()方法 要是线程运行中就返回true,新建或死亡就返回false.button_start.addActionListener(new ActionListener() { 
        public void actionPerformed(final ActionEvent e) { 
            if(!(t.isAlive());
                    { t = new Thread(this);
                     }
                 try
                    { t.start();  }
                     cath(Exception e){}
           } 
        });
      

  6.   

    button_start.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            new Text().t.start(); // 每次重新生成一个
           }
        });
      

  7.   


    t = new Thread(this);  他说 this 参数有问题