Explained – Android UI thread/main thread

Explained – Android UI thread/main thread


Hi guys… Codetutor here..and my name is
Anil Deshpande. In this particular video, i will be talking
about Android process and android run time. How the apps behave in the run time? and..
there is something called as UI thread, which is a very important concept. By default all
your tasks, all your important functionalities always run on the UI thread. So because of
this you have to be very careful the way you write your application.. other wise you
will encounter something called as Application Not responding dialog box. Without much further ado, let us get in to
this particular topic. At run time, an Android operating system is always hosting an app
as a process. So each app gets a dedicated process to run itself. So have a process,
then in the process, you have a android run time ART, which in earlier case used be a
Dalvik virtual machine.. which is DVM. At any point in time, it can host any number
of apps. ..for each have you have a separate process. If you consider one particular process, there
can be any number of threads in that particular process. And of the the thread, is the UI
thread or also referred to as main thread. And if you focus only on the UI thread, that
is the main thread, in this particular thread all of your UI tasks are getting executed. Like rendering a button, click of a button,
everything that is related to UI happens on this particular thread. And if you are not
careful enough, you might execute everything and anything on this particular threadd, which
is not a good idea. If you have any task, it always runs on the
UI thread. if that particular task or if that particular functionality is a very resource
intensive functionality.. which takes more than 5 seconds.. then it should not run on the
UI thread. Because you will be blocking the UI thread.
The best approach is you have to move it away from the UI thread. That is you should spawn
a separate thread, do your resource intensive work there and then hop back on to the UI
thread, so that your UI thread is not blocked. So let show you this through a actual demo,
so that it becomes very clear. You have a activity here. You have two buttons.
In the activity, I am logging the thread id, in this case it is 1. Because onCreate is
also one of the life cycle method of the activity. you can visit my another video in which I
have discussed about the life cycle of the activity, But here what I am concentrating
on is the id of the thread,.. this is main thread.. the onCreate method on which it is
executing is main thread. So you will see the id as any one particular number. In this
case it is 1. Now what i will do is.. I have these two buttons..
one particular button, when i click.. startThread.. I will start a for loop… that particular
for loop.. or infinite loop will basically… printout the thread id and the stop button
will basically will stop that particular loop. you have this particular Boolean variable
using which I can do that. Let me a implement onClick listener for those
buttons.. and in the overridden onClick method.. whenever I click on the start button, i make
the Boolean variable value true and i start the while loop. I will continue to run as
long as I don’t make it stop.. I don’t make Boolean variable false. so let me run this particular code.. you can
see that the thread id is 1. And… as soon as I click on the start thread, you can see
that t while loop is running. And the Id is one. Because that is what i am printing here thread
id. And when I click on the stop button..my loop should stop. because i am making mStopLoop
as false. But i am clicking here… many number of times… the loop is not stopping…. It will lead to a application not responding
because my loop is completely taking over the UI thread. So you should not do this. So how to solve this? Bu putting this particular
loop in a separate thread. What i will do is.. I will start a new thread.. and it will have
a Runnable instance … dot start.. and it will start the thread… and in the run method..
I will place this loop.. this particular loop… is running in a separate thread. And the id that it will display.. should be
different from the id.. that we are displaying here…so in this case everywhere we are getting
the id as one… but here.. we should get a different id inside the while loop. So let me run this. Thread id is one…when
the activity get’s started… that is the statement from the line number 22.. And now
i am starting the loop.. by clicking on the start thread button.. and you can see that
id is different… it is not 1.. it is 164… and now If i click on the other button…
the loop will stop… the loop has stopped. So.. can clearly make out that.. the Application
Not Responding… error that we were getting earlier.. is solved.. This is something that i wanted to emphasize
that.. the resource intensive operations… should not be done on the UI thread. So in a real application scenario… a resource
intensive operation.. could be something like… fetching a value from a web service.. because
of the network latency.. definitely it is going to take more than 5 seconds… Or it
could be something very complicated… operation.. like … image processing… or something… So you should avoid doing.. those kind of
really heavy resource intensive tasks on the UI thread. you should do it on a separate
thread and then.. update the UI. but updating the UI from a separate thread..
is little bit tricky… So that is something that I am going to talk about in the next
video. So keep watching the channel.. subscribe to
the channel.. let me know any new topic that you are interested in… Take care.. bye

27 thoughts on “Explained – Android UI thread/main thread

  1. i am using checkbox to on and off flashlight ………after starting service(flash light is on state) wen we close and open application the checkbox is showing unchecked state but still flash is in on state…..hw to solve this

  2. Please provide videos of parcelable in android.Because Parcelable is very much required when you use bulk of data to pass from one activity .Thanks in advance.

  3. Nice tutorial. Thanks. I am a little confused as to why the stop button didn't stop the logging until only when it was put in a new thread. I thought changing the Boolean value should have nothing to do with the thread. can you please explain?

  4. Make some videos on MVP, MVVM, MVA, Callback. Because most of the interviewers focuses on these topic now a days.I am writing these as your explanations are very nice.Thank you for helping.

  5. Maestro resuelveme una duda porfavor, cuando hablas de diferentes procesos (process /App) te refieres a diferentes apps? O estos procesos están en la misma aplicación???

  6. Hi sir,

    I'm facing some ANR problem while performing setadapter for Listview . My activity contains 3listview and many other (textview ..) and in that 1 Listview is always visible and consist 7 items and 2nd list view is visible only some cases and 3rd list view consist of huge items like I'm getting items of 3rd Listview from server by using asynctask and after getting items I'm not displaying it immediately.. initially my 3rd Listview is hidden when user click on title layout I'm performing setadapter for my 3rd Listview and making it visible . But the problem is here it's. Taking more time and showing ANR.

    I tried putting setadapter I'm asynctask , and showing progress dialog while setting adapter so that users can not perform other ui operation . But after setting adapter after progress bar dismiss also it's taking 20-30 seconds for making Listview visible ..

    Please help me..

  7. 2:30 demo
    4:04 onClick(View v) using Switch statement and Boolean flag
    4:24 putting loop on separate thread :
    new Thread(new Runnable {
    public void run () {
    while (mStopLoop) {
    ..
    }
    }
    }).start();
    5:15 now different Thread id showing in logcat
    5:37 separate thread use case advice

  8. 1) Sir, can't i check this while loop execution using my mobile phone instead of emulator
    2)And OnCreate() method itself is the "main Thread/UI thread right sir??

  9. Hey your video is good but when I am am making a sample then that time thread is automatic stopping after some
    interval of time, Please let me know why Its happening.

Leave a Reply

Your email address will not be published. Required fields are marked *