有程序如下:
class StopThread implements Runnable
{
public volatile boolean stop = false; public static void main(String args[]) throws Exception
{
Runnable r = new StopThread();
System.out.println("Starting thread...");
Thread threads = new Thread(r);
threads.start();
} public void run()
{
while (!stop)
{
System.out.println("Thread running...");
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Thread interrupted...");
}
}
System.out.println("Thread exiting under request...");
}
}我如何在外部正常停止这个程序?

解决方案 »

  1.   

    System.out.println("Starting thread...");
    Thread threads = new Thread(r);
    threads.start();
    threads.interrupted();  // 用这个就可以中断那个线程了。 
      

  2.   

    呀,说错了,应该是 threads.interrupt(); 这个方法,不好意思哦 :P
      

  3.   

    不错,System.out.println("Starting thread...");
    Thread threads = new Thread(r);
    threads.start();
    threads.interrupted(); // 用这个就可以中断那个线程了。  
      

  4.   

    我晕,有些人,整天就在CSDN上瞎混!我还以为上班,多认真呢!
      

  5.   

    Ctrl+C试试
    再不行只能杀进程了
      

  6.   

    给stop变量加个set方法,要停的时候(发一信息,或着按下某个键)把stop设置成true,线程就直接执行完了
      

  7.   

    问下,就是java中突然中断一个程序呢?