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 -
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.
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)
To force something to be executed on your main thread use
In order to determine if current thread is UI(main) thread:
Looper.getMainLooper().getThread() == Thread.currentThread();
Interesting question about threads in a service:
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