Explained - Android UI thread/main thread

Explained - Android UI thread/main thread - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Explained - Android UI thread/main thread, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article android, article Explained, article thread, article thread/main, article Tutorial, article UI, we can write this you understand. Well, happy reading.

Title : Explained - Android UI thread/main thread
link : Explained - Android UI thread/main thread

Baca juga


Explained - Android UI thread/main thread

Hi guys.

Codetutor here.

And my name isAnil Deshpande.

In this particular video, i will be talkingabout 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 allyour tasks, all your important functionalities always run on the UI thread.

So because ofthis you have to be very careful the way you write your application.

other wise youwill encounter something called as Application Not responding dialog box.

Without much further ado, let us get in tothis particular topic.

At run time, an Android operating system is always hosting an appas 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 aDalvik virtual machine.

which is DVM.

At any point in time, it can host any numberof apps.

For each have you have a separate process.

If you consider one particular process, therecan be any number of threads in that particular process.

And of the the thread, is the UIthread or also referred to as main thread.

And if you focus only on the UI thread, thatis 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 notcareful enough, you might execute everything and anything on this particular threadd, whichis not a good idea.

If you have any task, it always runs on theUI thread.

if that particular task or if that particular functionality is a very resourceintensive functionality.

which takes more than 5 seconds.

then it should not run on theUI 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 spawna separate thread, do your resource intensive work there and then hop back on to the UIthread, 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 isalso one of the life cycle method of the activity.

you can visit my another video in which Ihave discussed about the life cycle of the activity, But here what I am concentratingon is the id of the thread,.

this is main thread.

the onCreate method on which it isexecuting is main thread.

So you will see the id as any one particular number.

In thiscase 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 particularfor loop.

or infinite loop will basically.

printout the thread id and the stop buttonwill basically will stop that particular loop.

you have this particular Boolean variableusing which I can do that.

Let me a implement onClick listener for thosebuttons.

and in the overridden onClick method.

whenever I click on the start button, i makethe Boolean variable value true and i start the while loop.

I will continue to run aslong as I don't make it stop.

I don't make Boolean variable false.

so let me run this particular code.

you cansee that the thread id is 1.

And.

as soon as I click on the start thread, you can seethat t while loop is running.

And the Id is one.

Because that is what i am printing here threadid.

And when I click on the stop button.

My loop should stop.

because i am making mStopLoopas false.

But i am clicking here.

many number of times.

the loop is not stopping.

It will lead to a application not respondingbecause my loop is completely taking over the UI thread.

So you should not do this.

So how to solve this? Bu putting this particularloop in a separate thread.

What i will do is.

I will start a new thread.

and it will havea 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 bedifferent from the id.

that we are displaying here.

So in this case everywhere we are gettingthe id as one.

but here.

we should get a different id inside the while loop.

So let me run this.

Thread id is one.

Whenthe activity get's started.

that is the statement from the line number 22.

And nowi am starting the loop.

by clicking on the start thread button.

and you can see thatid 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 ApplicationNot Responding.

error that we were getting earlier.

is solved.

This is something that i wanted to emphasizethat.

the resource intensive operations.

should not be done on the UI thread.

So in a real application scenario.

a resourceintensive operation.

could be something like.

fetching a value from a web service.

becauseof the network latency.

definitely it is going to take more than 5 seconds.

Or itcould be something very complicated.

operation.

like.

image processing.

or something.

So you should avoid doing.

those kind ofreally heavy resource intensive tasks on the UI thread.

you should do it on a separatethread 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 nextvideo.

So keep watching the channel.

subscribe tothe channel.

let me know any new topic that you are interested in.

Take care.

bye.



Thus Article Explained - Android UI thread/main thread

That article Explained - Android UI thread/main thread This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Explained - Android UI thread/main thread the link address http://svedda79.blogspot.com/2016/11/explained-android-ui-threadmain-thread.html

0 Response to "Explained - Android UI thread/main thread"

Post a Comment