現在我用stop()這個方法停止線程﹐但這個方法已經過時了﹐而且會拋一個異常。如果不用stop()停止線程還有其它方法嗎?

解决方案 »

  1.   

    public void interrupt()中断线程。 具体的看API文档。
      

  2.   

    我有看API﹐中斷線程以后再重新啟動線程還是用start()嗎?
      

  3.   

    Yes 
    you are right!
      

  4.   

    package haha;public class ThreadDemo {
    public static void main(String [] args)
     {
    TestThread Test =new TestThread();
    Test.start();
    Test.interrupt();
    Test.start();
    Test.interrupt();
     }
    }class TestThread extends Thread
    {
    public void run()
    {   
    System.out.println("run:"+Thread.currentThread().getName());
    }
    }
    报错呀
      

  5.   

    boolean b=true;
    while(b){
       public void run(){}
    }
    在主线程里控制b的值.如果想中断的话.改成false
      

  6.   

    刚开始学java,对楼上说的不是很明白,有人给详细解释一下么~ 谢谢了~
      

  7.   


    上面写错了,看看这个!应该没问题!
    class TextThread
    {
    public static void main(String args[]){
    MyThread t=new MyThread();
    t.start();

    }
    }class MyThread extends Thread
    {
    int i=0;


    public void run()
    {
    while(i<10){
    System.out.println("I'm here!"+(i++));
    }}
    }
      

  8.   

    先interrupt那个线程  然后在线程所在的run方法中捕获异常  最后在catch中return;