-->

difference between thread/runnable, handler, runon

2020-07-18 06:35发布

问题:

i started learning android programming and am working on a small game. i heard that doing background operations or updates or downloading or what ever background and time consuming thing should not be done with ui thread and instead use thread/runnable or asynctask. but i cant do some things in threads like background connectivity to database where as this connectivity works with the remaining handler,runonuithread,asynctask.am greatly confused where to use which one. I have some questions 1.handler,runonuithread are both runs on ui thread, thread/runnable is a different thread and in async task, doinbackground method run on different thread and others methods like onprogressupdate,onpreexecute and onpostexecute run on ui thread. right? if that so i wrote a program to do database connectivity with thread/runnable it didnt worked but when i wrote it in doinbackground it worked. my confusin is that as both those methods run on different thread why this is happening. 2.what is the main difference btw those 4 and where are they applicable and not applicable. And also want to know what are the tasks which only ui thread can do. thanks in adv:)

回答1:

A Handler allows you to post messages to be executed on the main UI thread. Activity#runOnUiThread(Runnable) is a convenience method that uses a Handler internally to post a Runnable on the UI thread (see the source code). Handlers are often used to synchronize events generated on background threads with the main UI thread. For example, since Views and other UI widgets can't be modified directly on a background thread, the background thread might instead post a message that makes those modifications on the main UI thread instead of in the background.

An AsyncTask is a utility class that uses a thread pool to execute tasks and provides helpful callback methods (i.e. onPreExecute, onPostExecute, etc.) that are guaranteed to be executed on the main UI thread (in other words, it abstracts the idea of Handlers from the developer).



回答2:

Hello I will try to give you the most simple explanation!

Thread is a class that enables you to use the groovy Multitasking. It's a standard Java class. Every Java environment (in your case Android) can use it.

Runnable is an interface that does almost the same as the Thread class, but it's a bit simpler. In order to use it, you just need to implement the method run().

Handler is a more fancy, Android specific variant of the Thread class. There are certain specific functions and other useful stuff.

RunOnUiThread is a method which gives us access to the main thread or a.k.a. UI Thread in Android! Yes, again this simple word Thread! Because in Android you CAN'T modify the UI from another Thread, except the main (or a.k.a. UI) Thread, Google has provided a method to access this thread.

AsyncTask the most valuable and cool class to extend! It's designed to simplify our life as developers and do the best job in Android environment. It can be considered again as a Thread, but "simplified" and "unified". This means that using AsyncTask is simple, easy and very useful!


AsyncTask is c00l that's why I will give you a short description:

To use it, it's a best practice to extend AsyncTask in your class. Than you need to implement 3 methods! Yes, just three!

The first is used as a "preparation". In this method all the things that are happening are BEFORE the new, parallel thread is started.

The second is used to DO your JOB in a new thread!!1

The last (third) is used to execute some tasks JUST AFTER you complete your work in the newly created Thread.


If you are interested enough I'm sure that you can find a dozens tutorials and examples explaining this to you, but my opinion is that you just need to try them all!

REMEMBER The most important thing is to use a construction when it's most needed. Don't use complex structures when you can use a simpler one. And of course it's not coincidence that there is AsyncTask or Handler classes, they are meant to assist us!

Good luck and I hope this short and rubbish explanation is not too confusing.