2011-08-12 23 views
2

在下面的示例中,stop()方法是如何实现的?线程中的Stop()方法?

应该做些什么而不是使用stop()方法?

在我看来,当期望的状态被挂起时,线程将使用Object.wait等待。当线程恢复时,使用Object.notify来通知目标线程。但在下例中实施stop()的情况令人怀疑。

Class NewThread implements Runnable { 
    String name; // name of thread 
    Thread t; 
    boolean suspendFlag; 
    NewThread(String threadname) { 
     name = threadname; 
     t = new Thread(this, name); 
     System.out.println("New thread: " + t); 
     suspendFlag = false; 
     t.start(); // Start the thread 
    } 
    // This is the entry point for thread. 
    public void run() { 
     try { 
     for(int i = 15; i > 0; i--) { 
     System.out.println(name + ": " + i); 
     Thread.sleep(200); 
     synchronized(this) { 
      while(suspendFlag) { 
       wait(); 
      } 
      } 
     } 
     } catch (InterruptedException e) { 
     System.out.println(name + " interrupted."); 
     } 
     System.out.println(name + " exiting."); 
    } 
    void mysuspend() { 
     suspendFlag = true; 
    } 
    synchronized void myresume() { 
     suspendFlag = false; 
     notify(); 
    } 
} 

class SuspendResume { 
    public static void main(String args[]) { 
     NewThread ob1 = new NewThread("One"); 
     NewThread ob2 = new NewThread("Two"); 
     try { 
     Thread.sleep(1000); 
     ob1.mysuspend(); 
     System.out.println("Suspending thread One"); 
     Thread.sleep(1000); 
     ob1.myresume(); 
     System.out.println("Resuming thread One"); 
     ob2.mysuspend(); 
     System.out.println("Suspending thread Two"); 
     Thread.sleep(1000); 
     ob2.myresume(); 
     System.out.println("Resuming thread Two"); 
     } catch (InterruptedException e) { 
     System.out.println("Main thread Interrupted"); 
     } 
     // wait for threads to finish 
     try { 
     System.out.println("Waiting for threads to finish."); 
     ob1.t.join(); 
     ob2.t.join(); 
     } catch (InterruptedException e) { 
     System.out.println("Main thread Interrupted"); 
     } 
     System.out.println("Main thread exiting."); 
    } 
} 

回答

6

如果它返回run()function.no需要使用stop()函数,因为停止方法是由Java的过时和不安全的使用

1

调用stop方法将杀死它被称为线程。只有在没有使用继续线程正在进行的操作时,线程才能被杀死。当你将调用停止方法时,线程将停止执行并死亡。

最好允许线程完成其运行方法并杀死它的lef而不是强行杀死它。

+0

使用Thread.stop已过时,你的线程自动停止在考虑使用它之前,需要了解它的真实含义。 Thread.stop()并不总是停止一个线程。 ;) –

+0

@Peter:在什么情况下Thread.stop()不会杀死一个线程? – Logan

+0

Thread.stop()抛出一个ThreadDeath错误,如果你抓住ThreadDeath或Error或Throwable(更可能)它可以被记录和丢弃。请参阅我的答案中的文章了解更多信息。 –

1

调用stop()会触发一个异常/错误在线程中随机抛出。如果你有权访问线程的所有代码,它可以安全地使用,但如果是这种情况,你最好支持中断。

而不是Object.wait/notify,您可能会更好使用高级并发库支持,即使用可简化您的代码的Lock。

欲了解更多停止(); Does Thread.stop() really stop a Thread?

0

这取决于你的线索和他们必须做什么。

如果他们是工作人员,例如听一个tcp/ip套接字,那么你最好有一个volatile类型的布尔值,表示runher()方法应该继续。然后让你的类扩展线程实现一个pleaseStop()函数,将布尔值设为false,然后让你的run方法优雅地完成(你甚至可以清理你的资源)。另一方面,如果他们是工作量有限的工人,那么你应该等待他们准备好,使用join()函数。

0

Thread类中的stop方法已弃用且不安全,因为它会导致它解锁所有已锁定的监视器。

如何停止线程的一些建议可以在here找到。

0
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
    if (jToggleButton1.isSelected()) { 
     jToggleButton1.setBackground(Color.green); 
     jToggleButton1.setText("ON"); 
     //MainClass main = new MainClass(); 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        server = new ServerSocket(4400, 500); 
        do { 
         socket = server.accept(); 
         ClientHandler cliendHandler = new ClientHandler(socket); 
         cliendHandler.start(); 
        } while (true); 
       } catch (IOException ex) { 
       } 
      } 
     }).start(); 

    } else { 
     try { 
      server.close(); 
      jToggleButton1.setText("START SERVER"); 
      jToggleButton1.setBackground(Color.red); 
     } catch (IOException ex) { 
      Logger.getLogger(Server_Prog.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
}