Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I care about the difference between main thread and UI thread in Lollipop and beyond?

Before Lollipop, life was easy. You had a main thread - sometimes also called UI thread - in which all GUI stuff was done (and which you avoided under all circumstances for long-running operations to avoid any kind of hiccup), and you had your background threads where you did exactly this long-running stuff.

Now in Lollipop and later versions of Android, IIRC the term UI thread seems to point the user to the new RenderThread, a thread that is for example used to animate the ripples, hero elements between activities or any other kind of animation that needs to happen while the main thread processes input events or is busy creating new stuff for you in the background.

With Android Studio 1.3 all three thread types now got their own annotation, to denote that a particular piece of code should run on this particular thread. For me the question now is: Should I as app developer care to run anything on the UIThread, i.e. RenderThread, and as such ever use @UIThread in my application?

like image 826
Thomas Keller Avatar asked Jun 03 '15 01:06

Thomas Keller


2 Answers

The UIThread is still the main thread of execution for your application. While RenderThread is just a support processing thread to help with your application when there are delays in your main UI thread. As quoted from developer.android.com:

"RenderThread is a new system-managed processing thread that keeps animations smooth even when there are delays in the main UI thread."

For your question, should you care to run anything on the UIThread? The answer is yes, should you use RenderThread instead? If possible, yes.

like image 126
mtolingan Avatar answered Sep 20 '22 13:09

mtolingan


I remember on Chet Haase's presentation of RenderThread from the last year's Google IO. His statement was, that in first place we have to continue with MainThread as before. RenderThread is to be used for animations only. For instance, if we have a method like onDrawFrame() drawing animation at smooth 60 fps, we should rather call it in RenderThread, because MainThread can be slowed down by application logic or other stuff.

Back to your question. I would say use MainThread as before. If you experience some performance issues with animations, then try to move drawing parts of your code to RendererThread.

like image 45
sergej shafarenka Avatar answered Sep 19 '22 13:09

sergej shafarenka