8

Will a thread simply terminate after it has completed its execution?

This is how I initialize my thread:

    new Thread(new Runnable() {
        public void run() {

        }
    }).start();

Basically what I am trying to do simply execute a single task on a new thread and then terminate the thread. However, after some time I will start another one and so forth. I don't want to have a bunch of threads started and I am wondering if the thread will terminate itself after complete ting it's execution?

Thanks.

Georgi Angelov
  • 4,338
  • 12
  • 67
  • 96
  • 1
    Here ya go: http://stackoverflow.com/questions/8037129/what-happens-to-this-thread-runnable-at-the-end-once-it-is-completed – Justin Jasmann Mar 09 '14 at 01:41

2 Answers2

9

Yes. When run returns, the thread will stop.

To execute a single task in a thread on Android, you might want to consider using AsyncTask instead. AsyncTask is designed exactly for this purpose. It gives you a simple way to pass data to the other thread, and pass progress updates and a final result back to the main thread. Each AsyncTask is like a Thread, but with those extra features.

user253751
  • 57,427
  • 7
  • 48
  • 90
5

Will a thread simply terminate after it has completed its execution?

Yes, It will terminate and exit itself after completing the run() method

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105