上次没说清楚问题,不好意思
我就是想问一下,stop()这个方法,还有其它一些在5.0以后不建议使用的方法
用什么其它的方法替代了吗??
还是,因为安全原因就没有类似的功能了

解决方案 »

  1.   

    就是很普通的boolean判断啊。
    其实stop()这个方法没啥不好的,我知道为什么不推荐用了,不过你要是正确的使用stop绝对没错的。另:《thinking in java》对stop为什么被不建议使用,解释的完全错误.你可以用一用stop()很不错的函数
      

  2.   

    stop 替换为 while的boolean控制法
    暂停线程 替换为 wait()&notifyall(),或sleep()
      

  3.   

    设个标志就可以了import java.util.*;class CanStop extends Thread{
    private volatile boolean stop = false;
    private int counter =0;
    public void run(){
    while(!stop && counter <10000){
    System.out.println(counter++);
    }
    if(stop){
    System.out.println("Deteced stop");
    }
    }
    public void requestStop(){stop =true;}
    }public class Stopping{
    public static void main(String[] args){
    final CanStop stoppable = new CanStop();
    stoppable.start();
    new Timer(true).schedule(new TimerTask(){
    public void run(){
    System.out.println("Requesting stop");
    stoppable.requestStop();
    }
    },510);
    }
    }
      

  4.   

    boolean控制法,楼上的大牛已经帮你写了demo
      

  5.   

    谢谢两位
    那个Timer的用法我再研究研究,谢了
    给分了