Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @UiThread and @MainThread annotation in Android?

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

like image 990
Gil Julio Avatar asked Dec 15 '15 10:12

Gil Julio


People also ask

What is difference between UI thread and main thread?

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.

What is UI thread and main thread Android?

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() .

Is main thread same as UI thread in Android?

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.

What is the difference between service and thread in Android?

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.

What is the difference between main thread and UI thread in Android?

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.

What is main thread and background thread in Android?

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.

What is the difference between service and thread in Android?

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?

Why do we need a main thread in Android framework?

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.


2 Answers

@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

like image 101
vipin agrahari Avatar answered Oct 18 '22 03:10

vipin agrahari


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.

like image 2
Javier Marsicano Avatar answered Oct 18 '22 01:10

Javier Marsicano