`

终止线程

阅读更多
Thread类中有封装stop()方法终止线程,在程序中,使用了停用的方法并不意味着程序不会被执行,它仍然起作用。由于缺点不过,不提长使用而已。
看一下终止线程的示例:

package com.mz.xx.tb;

public class Deprecation2 extends Thread{
    
    boolean runnable = true;
    void stopThread(){
        runnable = false;
    }
    
    public void run() {
        
        while(runnable){
            System.out.println("Hi!!!!!");
        }
    }
    
    public static void main(String[] args) {
        Deprecation2 dt = new Deprecation2();
        dt.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dt.stopThread();
    }

}
分享到:
评论
2 楼 蜗牛/ 2011-09-07  
yahier 写道
如果是针对某些  一次性的耗时操作呢  没有while  的情况呢

currentThread.interrupt()
1 楼 yahier 2011-09-07  
如果是针对某些  一次性的耗时操作呢  没有while  的情况呢

相关推荐

Global site tag (gtag.js) - Google Analytics