29

I thought the @UiThread and @MainThread were the same thing.

Gil Julio
  • 812
  • 1
  • 9
  • 18
  • 1
    I didn't understand what is @MainThread annotation and what are its usage. Can you please explain it. It would be very helpful. – Sagar Trehan Jan 13 '16 at 14:26
  • Possible duplicate of [Difference between the main thread and UI thread](https://stackoverflow.com/questions/40784584/difference-between-the-main-thread-and-ui-thread) – avalancha Nov 30 '18 at 08:29
  • how does the annotation "checks"? throws an exception? `Thread annotations check if a method is called from` – Elad Benda Sep 01 '19 at 08:32

2 Answers2

18

@MainThread is the first thread that starts running when you start your application

@UiThread starts from Main Thread for Rendering user Interface

Also from Android Documentation

Note: The @MainThread and the @UiThread annotations are interchangeable so methods calls from either thread type are allowed for these annotations.

https://developer.android.com/tools/debugging/annotations.html#thread-annotations

vipin agrahari
  • 2,691
  • 3
  • 21
  • 31
  • 1
    It's the same thread. They just name it UI thread because that is where the rendering of the UI happens and the implementation might change in the future. – mr5 Apr 26 '17 at 06:38
  • 3
    That's not 100% correct, it's not just different naming, as the documentation says: > **Note:** Ordinarily, an app's UI thread is also the main thread. However, under special circumstances, an app's UI thread might not be its main thread; for more information, see [Thread annotations](https://developer.android.com/studio/write/annotations#thread-annotations) < Although I have to admit I don't know what those special circumstances are :D – avalancha Nov 30 '18 at 08:24
  • 3
    how does the annotation "checks"? throws an exception? `Thread annotations check if a method is called from` – Elad Benda Sep 01 '19 at 08:32
3

More complete explanation: It's possible for a UI thread to be different from the main thread in the case of system apps with multiple views on different threads. Therefore, you should annotate methods associated with an app's view hierarchy with @UiThread and annotate only methods associated with an app's lifecycle with @MainThread.