RuntimeException
RuntimeException

Reputation: 1241

What the UI thread will do if Activity/UI is idle for some(may be long) time

I think title itself says what the my question is...

AFAIK, in Java, when work of a Thread is completed i.e. run() method has completed executing, Thread itself will finish and dies.

So, When my Activity(I mean UI) is idle for a long time, what the UI thread will do? does it sleeps? or does it do any other work?

Thanks to all...

Upvotes: 0

Views: 1760

Answers (1)

cybersam
cybersam

Reputation: 66999

I believe the question is not really about Java threads in general, but about the Android "main thread" (also called the "UI thread").

To quote from the JavaDoc for the Android Handler:

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

In other words: The main thread is responsible for dequeuing messages/runnables from a queue and processing them. That main thread is blocked while the queue is empty (since there is nothing for it to do). If you use a Handler that was created in the main thread, that Handler's messages and runnables will actually be added to the same queue used by the main thread. Normally, the main thread will run as long as the process does.

Note: An Android app can actually have multiple processes, and each one will have its own main thread. However, most Android apps will only have one process (and therefore one main thread).

Upvotes: 4

Related Questions