开发者

A Thread in a service/activity

I have been looking at a lot of开发者_如何学JAVA articles on the topic of threads, and while maybe I do understand it, it does not feel that way. If an service is already in the background because it does not have a UI, why do people say to put a thread in a service to create a background task area? Also there is a main UI thread in your main activity. Where exactly is the code for this?


The Service itself is not a thread. It runs in the same process as its parent application. So, if your code runs in a service, and there is some processing that might hold it up, you would consider putting it in a separate thread in order to not block the other operations it carries out.

As for the UI thread, What is the Android UiThread (UI thread) might provide a good explanation (the first answer)


Just because there's no activity for it, and you can't see it, doesn't mean that code isn't currently being executed. Multithreading allows you to process tasks that take a long time, simultaneously.

For example, you are waiting to receive a response from the server. At this point your code is on HOLD, everything else is waiting for this response. But wait, you don't want the user to just be sitting there staring at a frozen screen, right? You want the user to be able to do other things or at least see some progress, right? So thats what multithreading allows. In this example, wait for data to download from the server, and update a progress bar on the main UI thread at the same time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜