如何停止或破坏正在运行的线程 [英] How to stop or destroy a running Thread
问题描述
我有一个活动的一个线程类,星的onCreate
。
I have one thread class which star in onCreate
of an activity.
class MyThread extends Thread
{
void run()
{
//My code which takes time.
}
}
//-------------------------- To run the thread
MyThread mThread = new MyThread();
mThread.start();
在某些外部事件,我需要停止/销毁当前运行的线程。
然后我创建MyThread的新副本,并调用启动
功能上的新的线程来运行它。
On some external events i need to stop/destroy currently running thread.
And Then i create a new copy of MyThread and call start
function on the new thread to run it.
但我不能够摧毁/停止previous正在运行的线程。
是否有任何的API,我们可以破坏正在运行的线程。
But i am not able to destroy/stop the previous running thread.
Is there any API by which we can destroy running thread.
推荐答案
您可以在不破坏......只有Android将停止线程,当需要..你不能停止或摧毁它..而不是尝试这样的..
you cannot destroy...only the android will stop the thread when requires.. you cannot stop or destroy it.. instead try like this..
class MyThread extends Thread
{
void run()
{
while(bool){
//My code which takes time.
}
}
}
// --------------------------要运行的线程
//-------------------------- To run the thread
MyThread mThread = new MyThread();
mThread.start();
现在当u要停止线程...变化布尔值设置为false
now when u want to stop the thread... change bool value to false
bool=false;
现在的code犯规运行...然后你就可以开始新的线程...
now your code doesnt run... and you can start new thread...
这篇关于如何停止或破坏正在运行的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!