Syam S Syam S - 3 years ago 184
Android Question

Android -Activity - Thread Basics

I am still confused at threads. I learned java through android, may be i am lacking some basics. Still..
I could see some answers which points to threads, UI thread, background thread , main thread etc. ( mostly on asyncTasks / updating UI from background thread etc. )
I think I am lacking some idea on these. Could anyone give me a complete idea around these? Or some links at least.

It would be great if the answer covers the following cases:

Which are all the threads that's involved -


  • When I am running an activity ( seting a content view , attaching
    some buttons, some dialog messages )

  • Activity - with async task

  • Background Service

  • An http Call


Answer Source
  • UI thread (main thread) - it is crucial to instantiate (add) all UI elements on this thread, that is why it has a nickname UIThread
  • AsyncTask - has methods doInBackground, onPostExecute, etc. Sort of its own lifecycle
  • Background Service (service) A service runs by default in the same process as the application. in its own thread. (as pointed out by @MisterSmith) A service runs by default in the same process as the application. in its own thread. Therefore you need to use asynchronous processing in the service to perform resource intensive tasks in the background. Services which run in the process of the application are sometimes called local services. Thought you can specify to run a Service in its own process:

Running a service in its own process will not block the application in case the service performs long running operations in its main thread. But as the services runs in its own process you need to use some interprocess communication (IPC) to communicate to your service from other parts.

  • HTTP call executed using HttpClient (from docs:

Thread safety of HTTP clients depends on the implementation and configuration of the specific client.

), has to be executed on non-UI thread by using new Thread(new Runnable(...)).start();, otherwise you will get NetworkOnMainThreadException thanks to @vikram. But it seems that HttpClient.execute() method is executed in a thread, that is why it requires a handler as one of the parameters in order to pass the result to the corresponding handler (handler runs on UI thread and can update widgets (UI elements) as it is needed)

Extra:

  • To force something to be executed on your main thread use yourContextInstance.runOnUiThread(new Runnable(....));

  • In order to determine if current thread is UI(main) thread:

Looper.getMainLooper().getThread() == Thread.currentThread();

Interesting question about threads in a service:

Does a runnable in a service run on the UI thread

Why UI thread is responsible for all this?

Because UI thread is in charge of dispatching the events to the appropriate widgets (UI elements), which includes the drawing events

Sources: this that and a little bit of that and some of that

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download