JAVA终止线程

来源:互联网 发布:广东精点数据 编辑:程序博客网 时间:2024/06/06 00:50

我们一直以来都有一个错误的理解,认为interrupt会使线程停止运行,但事实上并非如此调用一个线程的interrupt方法会把线程的状态改为中断态,但是interrupt方法只作用于那些因为执行了sleep、wait、join方法而休眠的线程,使他们不再休眠,同时会抛出InterruptedException异常

        比如一个线程A正在sleep中,这时候另外一个程序里去调用A的interrupt方法,这时就会迫使A停止休眠而抛出InterruptedException异常;而如果线程A没有处于上面提到的三种休眠状态时被interrupt,这样就只是把线程A的状态改为interrupted,但是不会影响线程A的继续执行。
        如何停止一个线程呢?用stop方法吗?肯定不行,这个方法由于不安全已经过时,不推荐使用。


Java Thread Stop方法以及替换实现

本文来自:http://blog.csdn.net/mal327/article/details/6962727
 
 Stop方法不推荐使用,我给个具体的例子:
public class DeprecatedStop extends Object implements Runnable {
    public void run() {
        int count = 0;
        while ( count <20 ) {
            System.out.println("Running ... count=" + count);
            count++;
            try {
                Thread.sleep(300);
            } catch ( InterruptedException x ) {
                // ignores
            }
        }
        
        // the code maybe not executed 
        System.out.println("stoped");
    }
    public static void main(String[] args) {
        DeprecatedStop ds = new DeprecatedStop();
        Thread t = new Thread(ds);

        t.start();
        try {
            Thread.sleep(2000);
        } catch ( InterruptedException x ) {
            // ignore
        }
        // Abruptly stop the other thread in its tracks!
        t.stop();
    }
}
 
 可能的运行结果:
Running ... count=0
Running ... count=1
Running ... count=2
Running ... count=3
Running ... count=4
Running ... count=5
Running ... count=6
可以发现程序中的打印stoped并没有执行,所以说如果在程序中有其他操作,如果线程突然stop是会带来严重的影响的。所以怎么也应该使用该操作。当然如果是我上面的程序代码突然stop的影响其实是没有的,但是如果是其他打开文件最后需要释放或者什么的就会带来严重的影响了。
 
如何在程序中对其进行停止呢?
 
public class AlternateStop extends Object implements Runnable 
{
    //run方法的循环条件,通过改变此变量使run方法结束,以达到结束线程的目的
   private volatile boolean stopRequested;
    private Thread runThread;
    public void run() {
        runThread = Thread.currentThread();
        stopRequested = false;
        int count = 0;
        while ( !stopRequested ) {
            System.out.println("Running ... count=" + count);
            count++;
            try {
                Thread.sleep(300);
            } catch ( InterruptedException x ) {
                Thread.currentThread().interrupt(); // re-assert interrupt
            }
        }

        
        System.out.println("stoped");
    }
    public void stopRequest() {
        stopRequested = true;
        if ( runThread != null ) {
            runThread.interrupt();
        }
    }

    public static void main(String[] args) {
        AlternateStop as = new AlternateStop();
        Thread t = new Thread(as);

        t.start();
        try {
            Thread.sleep(2000);
        } catch ( InterruptedException x ) {
            // ignore
        }

        as.stopRequest();//改变stopRequested变量的值,使run方法不再循环,结束run方法即可停止线程
    }
}
可能的运行结果如下:
Running ... count=0
Running ... count=1
Running ... count=2
Running ... count=3
Running ... count=4
Running ... count=5
Running ... count=6
stoped
这样我们就解决了强制 stop的问题。

0 0
原创粉丝点击