I thought the @UiThread and @MainThread were the same thing.
In Android the main thread and the UI thread are one and the same. You can use them interchangeably. In Android each app gets a dedicated process to run. Thus the process will be having a main thread.
Main Thread: The default, primary thread created anytime an Android application is launched. Also known as a UI thread, it is in charge of handling all user interface and activities, unless otherwise specified. Runnable is an interface meant to handle sharing code between threads. It contains only one method: run() .
As such, the main thread is also sometimes called the UI thread. However, under special circumstances, an app's main thread might not be its UI thread; for more information, see Thread annotations. The system does not create a separate thread for each instance of a component.
Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.
In Android the main thread and the UI thread are one and the same. You can use them interchangeably. In Android each app gets a dedicated process to run. Thus the process will be having a main thread.
This thread is called main thread. Background or worker thread can be created within the app to run long running tasks. Main thread is also called UI thread as all UI components run on the main thread. But in system apps, UI thread can be different from main thread if views run on different threads.
In short, the main difference between Service and Thread is that, Service runs on Main (UI) thread and Thread runes on its own thread. If we are using Service for long tasks, then it may cause block Main UI Thread. Do all components in Android run on the same thread?
When Android framework is being started for the first time, it too runs as an application, but this application is special (for example: has privileged access). Part of this “specialty” is that it needs a specially configured “main” thread.
@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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With