73

The Android doc says "Like activities and the other components, services run in the main thread of the application process."

Is the main thread here the same thing as UI thread?

2

6 Answers 6

85

Looks like it. Quoted from http://android-developers.blogspot.com/2009/05/painless-threading.html: "When an application is launched, the system creates a thread called "main" for the application. The main thread, also called the UI thread...", Official API document.

3
  • 15
    Note: This is only true on Android. In other Java apps your "main" thread is specifically NOT the UI thread and in theory you shouldn't even create your GUI on the main thread (nearly everyone does though and I don't think it's killed anyone yet)
    – Bill K
    Commented Mar 8, 2011 at 11:01
  • 8
    Please note that this answer is not completely accurate (due to inaccuracy in the official docs it quotes). Complete answer to this question is available here: stackoverflow.com/a/40795895/2463035
    – Vasiliy
    Commented Dec 19, 2016 at 18:43
  • Updated statement from the API document pointed in the answer (developer.android.com/guide/components/…) : However, under special circumstances, an app's main thread might not be its UI thread; for more information, see Thread annotations(developer.android.com/studio/write/…).
    – userv
    Commented May 24, 2021 at 18:18
25

UI Thread and Main Thread are same only in Android.

The Main thread, that is responsible for handling the UI events like Draw, Listen and receive the UI events.

Ans also it is responsible for interact with running components of the UI toolkit for the corresponding application that belongs to.

When an User event occurs in the application, the Main thread *

need to add the event in the queue -> intimate about the event to appropriate View -> change the state of the view -> redraw the view according to the state changes -> waiting for the response for the particular event action -> after intimated and event action completed need to delete the event in the queue.

*

The above every actions are handled by the Main thread (Not only the above operation, it is a one of the operation handled by the UI Thread), So if our application fails to respond the event about 5 seconds android will shows the error "not responding".

So only it is widely suggested to do the light processes in the UI thread.

Hope this answer is somewhat detail and helpful to the new android bees like me. I just shared what i learned about UI Thread. If i went wrong in anywhere please don't hesitate to recorrect me.

14

Basically Main Thread is Ui Thread.
However sometimes they can be different treads!
It is possible for system apps with multiple views on different threads.
Also if you use support annotations note that both @MainThread and @UiThread are available at the same time.
Here with the first one you annotate methods associated with the App life cycle and with the second one methods that are in charge of view hierarchy.
https://developer.android.com/studio/write/annotations.html

0
11

The "main application thread" is sometimes called the "UI thread".

4
  • can u pls suggest me if mainthread is UI thread and docs says that services run on Main thread then why services doesnot block UI thread how services work ?
    – Erum
    Commented Oct 6, 2015 at 10:21
  • 1
    @Erum: "docs says that services run on Main thread" -- no, the documentation does not. In Java programming, objects do not run on threads. Methods run on threads. The lifecycle methods of a Service (onCreate(), onStartCommand(), onBind(), onDestroy()) are called on the main application thread, and any work done from those methods on the main application thread will block the UI from updating. Services usually fork threads for long-running tasks. Some classes, like IntentService, provide such a thread for you; otherwise, you fork your own. Commented Oct 6, 2015 at 10:24
  • "main application thread" is sometimes called the "UI thread". So could you please explains what are the cases in which main thread is called ui thread and in which not Commented May 25, 2016 at 6:26
  • @AmandeepRohila: The terms "main application thread" and "UI thread" are used by different authors to refer to the same thread. You would have to ask those authors why they choose to use the specific term that they do. I usually stick to "main application thread", as that thread is tied to more than UI. Commented May 25, 2016 at 11:01
2

Every Activity has its own UI thread. As soon as the VM boots up, System Server is started by the Zygote. All other services like Activity Manager Service are started in new threads by the System Server.

1
  • 3
    I don't think this is quite true: (unless you meant 'Application' instead of 'Activity'): "The system does not create a separate thread for each instance of a component." - see developer.android.com/guide/components/… - of course you can request each component (Activity, Service, etc) has its own process in the manifest file, but it isn't done that way by default. Commented Aug 22, 2012 at 1:13
1

Yes. main thread is UI thread.

See this tutorial for full details about background processing in android

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.