Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does thread die when activity is finished?

If i start a background thread, what will happen if the activity that is started from finishes() before the thread terminates. Will the thread terminate as well or will it stay alive?

 new Thread(new Runnable() {
                public void run() {
                    while (mProgressStatus > 0) {


                        // Update the progress bar
                        mHandler.post(new Runnable() {
                            public void run() {
                                progressbar.setProgress(mProgressStatus);
                            }
                        });
                    }
                }
            }).start();
like image 786
Jake Avatar asked Apr 29 '13 03:04

Jake


People also ask

What happens when a thread is finished?

Short Answer: When a thread has finished its process, if nothing else holds a reference to it, the garbage collector will dispose of it automatically.

What happens to thread pool after it finishes its task?

Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task. There is only one thread pool per process.

What causes thread to die?

A thread dies naturally when its run() method exits normally. For example, the while loop in this method is a finite loop--it will iterate 100 times and then exit. A thread with this run() method will die naturally after the loop and the run() method completes.

Does a thread automatically be killed?

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume() and stop() were used to manage the execution of threads.


1 Answers

Threads run idependently from their parents. Thread dies when it returns from Thread.run() back to JVM normally or due to an uncaught exception.

like image 160
Evgeniy Dorofeev Avatar answered Nov 02 '22 15:11

Evgeniy Dorofeev