恰好这个线程可运行的到底是什么,一旦它完成? [英] What happens to this thread runnable at the end once it is completed?

查看:117
本文介绍了恰好这个线程可运行的到底是什么,一旦它完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个线程,下载一些图片,从该服务器。因此,一旦它下载的图片我所说的处理程序,并进行UI updation。如此以来,停止()的线程是pcated我不能用它去$ P $。我有两个问题。

  1. 发生此主题到底是什么?(指后,我打电话处理方法恰好是什么)。
  2. 或如何停止该线程不使用的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.

  1. What happens to this thread in the end?(means after I call the handler method what happens to it).
  2. 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.

这篇关于恰好这个线程可运行的到底是什么,一旦它完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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