android线程:停止计算量大的线程
我正在我的 Android 应用程序中进行一些繁重的计算。由于我不想阻塞 UI 线程,因此我在一个扩展了 Thread 的单独 WorkerThread 中进行计算。这很好用。但我无法停止线程。停止线程的正常方法是使用interrupt()或一个易失性变量来告诉线程它应该结束。但这仅适用于具有某种循环的线程。我正在使用外部库在线程中进行计算,并且无法将 isInterrupted() 检查放入该库中。停止线程的糟糕方法是使用 stop()-Methode,该方法已被弃用,但在我的情况下是正确的方法,因为线程仅操作内部值。不幸的是,Android 不支持 stop(),因为它不支持已弃用的线程方法。
10-18 10:26:39.382: ERROR/global(13919): Deprecated Thread methods are not supported.
10-18 10:26:39.382: ERROR/global(13919): java.lang.UnsupportedOperationException
你现在还有其他方法来停止android中的线程吗? 提前致谢! Tobias
PS:一些代码来显示问题:
public void method()
{
WorkerThread t = new WorkerThread();
t.start();
synchronized (this)
{
try
{
wait(1000);
} catch (InterruptedException e)
{
Log.e("",e.toString(),e);
}
}
//t.stop(); //not supported in android
//t.interrupt(); //can not be checked by the thread
}
class WorkerThread extends Thread
{
public void run()
{
externalLibrary.heavyComputation();
}
}
I am doing some heavy computation in my android application. Since i dont want to block the UI-thread, I do the computation in a seperate WorkerThread, which extends Thread. This works fine. But i have trouble stopping the thread. The normal way to stop a thread is either using interrupt() oder a volatile variable to tell the thread, that it should end. But this works only in Threads with some kind of loop. I am doing computation in the thread by using external libraries and I cannot put the isInterrupted()-check into this library. The bad way to stop a thread is using the stop()-Methode, which is deprecated, but in my case the right way, since the thread manipulates only internal values. Unfortunatly Android doesnt support stop(), since it doesn't support deprecated thread methods.
10-18 10:26:39.382: ERROR/global(13919): Deprecated Thread methods are not supported.
10-18 10:26:39.382: ERROR/global(13919): java.lang.UnsupportedOperationException
Do you now any other method to stop a thread in android?
Thanks in advance!
Tobias
PS: some code to show the problem:
public void method()
{
WorkerThread t = new WorkerThread();
t.start();
synchronized (this)
{
try
{
wait(1000);
} catch (InterruptedException e)
{
Log.e("",e.toString(),e);
}
}
//t.stop(); //not supported in android
//t.interrupt(); //can not be checked by the thread
}
class WorkerThread extends Thread
{
public void run()
{
externalLibrary.heavyComputation();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,除非您的外部库提供某种协议来停止计算,否则无法优雅地停止计算。许多写得好的库都会提供这样的协议,所以看看它的 api。它还可能尊重
interrupt()
调用。可能您已经检查过它,但退出可能还需要一些时间。Generally, unless your external library provides some protocol to stop computation there is no way to stop it gracefully. Many well written libraries will do provide such protocol, so look at its api. Also it may respect
interrupt()
call. May be you already checked it but it may also take some time before it exits.看看
AsyncTask
。这是执行后台任务的更合适、更舒适的方式。正如您可以在文档中阅读的那样:
Look at
AsyncTask
. It is more proper and far more comfortable way to implement background tasks.As you can read in documentation: