8

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();
Andro Selva
  • 53,910
  • 52
  • 193
  • 240

2 Answers2

8

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.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • So what if in some case I want to stop this thread in the middle. How do i do it in android – Andro Selva Nov 07 '11 at 13:36
  • 1
    If it had a permanent loop, you'd have to have a boolean condition like `while(a){// do everything }` and set `a` to false when you want it to die. If your `myDownloadMethod()` has no way of being interrupted and canceled, then you'll have to create one or have a timeout to just let it die. You're not supposed to just cut a thread in the middle of it's execution. Best practice is to have it run to it's end. Very often, you can use `Thread.interrupt()` for this purpose. – DeeV Nov 07 '11 at 13:39
  • @DeeV check out the ExecutorService and Executors. Lots of thought when in to those guys and I have all to often seen developers reimplement what those classes do. – Adam Gent Nov 07 '11 at 13:48
  • That looks like something that may be helpful for this. It's probably what `AsyncTask` wraps around since it works similarly. – DeeV Nov 07 '11 at 13:54
0

You should look into the ExecutorService and Executors.

If its the same as regular Java then it provides some powerful thread management including shutting down all the threads at once.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • He is using Android, so AsyncTask is probably more appropriate then Executors. – toto2 Jul 16 '12 at 12:08
  • Shutting down threads with `interrupt` as you suggest is not trivial since the threads have to be modified to correctly handle the interrupts. – toto2 Jul 16 '12 at 12:09
  • @toto2 yes I agree. However Anything is better than just plain Thread. You should post an answer with AsyncTask (at the time I did not know about it). – Adam Gent Jul 16 '12 at 20:10