Thread.stop()最后 [英] Thread.stop() and finally

查看:84
本文介绍了Thread.stop()最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为ThreadClass的以下类(如您所见,它是一个线程),其结构类似于以下内容

I created a following Class named as ThreadClass (which is a thread as you can see),its structure is something like the following

class SomeTask implements Runnable
{
    boolean someCondition=true;
    public void run() {
        try
        {
            while(someCondition)
            {
            //Here goes the Process Code
            }
        }
        catch(Exception errorException)
        {
            //Catching the Exception
        }
        finally
        {
            ////I expect that this finally should run every time ,whatever happens in the world
        }
    }

}

我的问题是关于最终块和 stop()方法

My question is about the finally block and the stop() method

由于上述类正在实现Runnable,因此我可以通过调用 start()方法来创建此类的对象并启动其线程.通过使用 stop()(是的,我知道它已弃用)方法来实现此线程.

As above class is implementing Runnable, so I can create the object of this class and start a thread of it by calling start() method.I am also aware of the fact that I can stop this thread by using stop() (Yes , I know it is deprecated) method .

我想说明的是,如果我需要以某种方式在ThreadClass的对象上调用stop方法,那么即使线程停止了运行,我也可以依靠 finally 块来执行当我在finally块中完成一些重要的关闭操作时,调用 stop().

What I want to clarify myself is that, if somehow I need to call the stop method on the ThreadClass's object, then can I rely on the finally block to execute even if the thread is stopped by calling stop() as I am doing some important closing things in the finally block.

推荐答案

Thread#stop通过抛出ThreadDeath异常来工作,它不会像System.exit摧毁JVM的方式立即消除线程. ThreadDeath甚至可以被捕获,尽管这不是一个好主意.因此,try块仍然很重要.

Thread#stop works by throwing a ThreadDeath exception, it doesn't obliterate the thread instantaneously the way System.exit blows away the JVM. ThreadDeath can even be caught, although that's not a good idea. So try blocks are still relevant.

但是,使该问题变得复杂的是,如果该线程多次调用了stop,那么,如果该线程处于finally块中时调用了第二个stop,则它可能会从finally块中抛出,从而finally块将被抛出.然后不完整.而且,如果线程的清理需要一段时间,那么停止可能会被多次调用.

However, complicating this is that if the thread has stop called on it multiple times then, if the second stop is called when the thread is in a finally block then it could be thrown from the finally block so that the finally block would not complete then. And if the thread's cleanup takes a while then it might be likely that stop could be called more than once on it.

或者即使您只调用一次stop,如果在调用stop时线程恰好已经在执行它的finally块,那么stop将干扰完成finally块.

Or even if you only call stop once, if at the time that stop is called the thread happens to be already executing its finally block, then the stop would interfere with completing the finally block.

这类似于关于线程原语的技术说明弃用指出:

1)线程几乎可以在任何地方引发ThreadDeath异常.考虑到这一点,必须对所有同步的方法和块进行详细的研究.

1) A thread can throw a ThreadDeath exception almost anywhere. All synchronized methods and blocks would have to be studied in great detail, with this in mind.

2)从第一个线程清除时(在catch或finally子句中),线程可以引发第二个ThreadDeath异常.必须重复进行清理,直到成功.确保这一点的代码将非常复杂.

2) A thread can throw a second ThreadDeath exception while cleaning up from the first (in the catch or finally clause). Cleanup would have to repeated till it succeeded. The code to ensure this would be quite complex.

因此,在某些情况下存在问题,要确保正确完成清理工作将非常困难. James的评论是正确的,如果可能的话,您应该对此类事情使用中断,以便线程可以可靠地完成其业务.

So there are some cases that are problematic, it would be very difficult to make sure cleanup gets done properly. James' comment is correct, if at all possible you should use interruption for this kind of thing so that the thread can reliably finish its business.

这篇关于Thread.stop()最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆