我有这个线程,下载一些图片,从该服务器。因此,一旦它下载的图片我所说的处理程序,并进行UI updation。如此以来,停止()的线程是pcated我不能用它去$ P $。我有两个问题。
发生此主题到底是什么?(指后,我打电话处理方法恰好是什么)。 或如何停止该线程不使用的stop()?下面是我的code。
处理程序=新的处理程序()
{
公共无效的handleMessage(信息MSG)
{
如果(msg.what == 0)
{
// UI Updation发生。
}
}
};
最终线程t =新主题(新的Runnable(){
公共无效的run(){
Log.i(内螺纹,下载图像......);
myDownlaodMethod();
handler.sendEmptyMessage(0);
}
});
t.start();
解决方案
该线程将结束,死在它自己的。你不必给自己结束。你将无法重新启动它,而无需创建一个新的发
对象。垃圾收集器将处理任何内存需要释放。该对象将保留在内存中,只要你持有一个参考吧。取出参考,并垃圾收集器将删除的对象,就像任何其他的。
I have this thread which downloads a few images from the server. So once it downloads the images I call the handler and carry on UI updation. So since stop() for thread is deprecated I am not able to use it. I have two questions here.
What happens to this thread in the end?(means after I call the handler method what happens to it). OR how do I stop this thread without using stop()?Here is my code.
handler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==0)
{
//UI Updation takes place.
}
}
};
final Thread t = new Thread(new Runnable() {
public void run() {
Log.i("Inside Thread", "Downloading Images...");
myDownlaodMethod();
handler.sendEmptyMessage(0);
}
});
t.start();
解决方案
The thread will end and die on it's own. You don't have to end it yourself. You won't be able to restart it without creating a new Thread
object. The garbage collector will handle whatever memory needs to be release. The object will stay in memory as long as you hold a reference to it. Remove the reference, and the garbage collector will remove the object just like any other.