Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to this thread runnable at the end once it is completed?

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();
like image 352
Andro Selva Avatar asked Nov 07 '11 13:11

Andro Selva


1 Answers

The thread will end and die on its 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.

like image 73
DeeV Avatar answered Nov 06 '22 17:11

DeeV