我正在我的 android 应用程序中进行一些繁重的计算。因为我不想阻塞 UI 线程,所以我在一个单独的 WorkerThread 中进行计算,它扩展了 Thread。这工作正常。但我无法停止线程。停止线程的正常方法是使用中断()或易失性变量来告诉线程它应该结束。但这仅适用于具有某种循环的线程。我正在使用外部库在线程中进行计算,我无法将 isInterrupted()-check 放入该库中。停止线程的坏方法是使用 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中的线程吗?提前致谢!托拜厄斯
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();
}
}