停止/销毁线程 [英] Stopping/Destroying a Thread

查看:189
本文介绍了停止/销毁线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,启动一个线程和一个Runnable像这样。

I have a Service that launches a Thread and a Runnable like so.

t = new Thread(new Runnable() {
    public void run() {
        doSomething();
    }
});

t.start();

的原因线程执行异步任务的 DoSomething的()。现在让我们不要担心其他类AsyncTask的。我已经尝试过了,它不适合我的情况下工作。编辑:我不能使用AsyncTask的,因为它意味着只有UI线程。这片code有一个服务里面工作,所以没了,没有的AsyncTask:(

The reason for the thread is to perform an Async task doSomething(). For now lets not worry about the other class AsyncTask. I have tried it and it does not work for my case. I can't use AsyncTask because it is meant for the UI thread only. This piece of code has to operate inside a Service, so nope, no AsyncTask :(

DoSomething的()包含了一些外部库,所以我遇到的问题是,它可能会被挂在命令中的一个,不返回任何值(因此没有错误检查,甚至可以做到)

doSomething() contains some external libs so the issue I am having is that it can potentially be hung at one of the commands, without return any value (hence no error checking can even be done)

要解决这个问题,我会想,在某个时间点,破坏服务。

To work around this, I will want to, at some point of time, destroy the Service.

stopService(new Intent("net.MyService.intent));

这工作得很好,并且很容易在手机上进行验证。然而,这是上面创建的线程将继续,即使是催生它的服务被破坏运行。

This works fine and is easily verified on the phone. However, the Thread which was created above will continue to run even when the Service that spawned it is destroyed.

因此​​,我要寻找正确的命令,在服务的的onDestroy插入()这将清理线程我。

I am thus looking for the correct commands to insert in the Service's onDestroy() which will clean up the Thread for me.

t.destroy();
t.stop();

都去preciated并导致应用程序崩溃。

are both depreciated and cause application crashes.

我把这个code从什么地方

I took this code from somewhere

@Override
public void onDestroy() {

    Thread th = t;
    t = null;

    th.interrupt();

    super.onDestroy();
}

但它仍然无法正常工作,该线程将继续运行。任何帮助球员?

but it still does not work, the thread continues to run. Any help guys?

推荐答案

线程破坏停止方法固有的死锁倾向和不安全的。它们的存在也给了幻觉,有可能是立即停止另一个线程,当别的东西告诉它以某种方式。

The thread destroy and stop methods are inherently deadlock prone and not safe. Their existence also gives the illusion that there might be some way of halting another thread immediately when something else tells it to.

我理解你的想法,从您的角度来看他们是一个主线程,在该线程没有收到它的响应的工作线程在一段时间,你想杀死它,并重新启动它,不关心什么就看的。但之所以这些方法都去precated是你的的关心线程是最多。许多。

I understand your thinking, from your point of view their is one main thread, and when this thread hasn't received a response from it's worker thread in a while you'd like to kill it and restart it, without caring what it's up to. But the reason those methods are deprecated is you should care what the thread is up to. A lot.

如果该线程有大约以后需要使用一个变量的锁?如果一个线程的文件处理打开的是什么?在所有这些情况下,更多的人,简单地停止线程在它的当前操作会留下的东西乱七八糟 - 很可能你的应用程序只会进一步崩溃的路线

What if the thread has a lock around a variable you need to use later? What if a thread has a file handle open? In all these cases, and many more, simply stopping the thread at it's current operation would leave things in mess -- quite likely your application would just crash further down the line.

因此​​,为了使一个线程被中断或取消,能够或停止的,它必须管理这本身。如果一个线程或操作提供没办法为自己被打断,那么你就不能中断它 - 假定这样做是不安全的。

So in order for a thread to be interruptible or cancel-able or stoppable, it has to manage this itself. If a thread or operation provides no way for itself to be interrupted, then you cannot interrupt it - it is assumed to do so would be unsafe.

如果您可运行实际上就是

If you runnable is literally

public void run() {
   doSomething();
}

那么有没有办法打断它。人们希望,如果 DoSomething的是一个长期操作,有可能是一种方法来与它进行交互增量的东西,如

then there is no way to interrupt it. One would hope that if doSomething were a long operation that there might be a way to either interact with it incrementally with something like

public void run() {
   while (running) {
       MyParser.parseNext();
   }
}

或者能够通过参考其指示线程是否被中断或没有,并希望该方法将中断本身在合适的位置,以通过在变量中。

or to be able to pass in a variable by reference which indicates whether the thread is interrupted or not, and hopefully the method would interrupt itself at suitable location.

还记得阻止操作阻塞。有没有办法来解决这个问题,则无法取消它的一部分的方式,通过。

Remember a blocking operation is blocking. There is no way to get around that, you cannot cancel it part way through.

这篇关于停止/销毁线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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