线程/可运行,处理程序,runonuithread,AsyncTask的区别 [英] difference between thread/runnable, handler, runonuithread, asynctask

查看:337
本文介绍了线程/可运行,处理程序,runonuithread,AsyncTask的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习编程机器人和我工作的一个小游戏。我听说做后台操作或更新,或下载或什么都背景和耗费时间的事情不应该与UI线程来完成,而使用线程/ Runnable或AsyncTask的。但我不能做像后台连接线程一些事情,数据库,在那里,因为这连通,其余处理程序,runonuithread,asynctask.am极大困惑的作品在哪里使用哪一个。
我有一些疑问
1.handler,runonuithread都在UI线程,线程/可运行两个运行在不同的线程和异步任务,在不同的线程和其他方法,如onprogressupdate doInBackground方法来看,在preexecute和onpostexecute在UI线程上运行。权利?
如果让我写了一个程序做线程/可运行它没有工作数据库连接,但是当我在doinbackground写它的工作。我confusin是,作为这两个方法在不同的线程上运行为什么发生这种情况。
2.什么是主要的区别BTW那些4和他们在哪里适用,不适用。
而且也想知道是什么,只有UI线程可以执行的任务。
感谢广告:)

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:)

推荐答案

A 处理程序允许你发布消息到主UI线程上执行。 活动#runOnUiThread(Runnable接口)是使用简便方法。处理程序在内部发布可运行在UI线程上(请参阅<一个href=\"https://github.com/android/platform_frameworks_base/blob/lollipop-release/core/java/android/app/Activity.java#L5243-L5249\"相对=nofollow>来源$ C ​​$ C )。 处理程序,则经常用来同步与主UI线程上的后台线程生成的事件。例如,由于查看和其他UI部件不能在后台线程上直接修改,后台线程可能会改为发布消息,使主UI这些修改线程,而不是在后台运行。

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.

这是的AsyncTask 是使用一个线程池来执行任务,并提供有用的回调方法(即上preExecute onPostExecute 等)保证将在主UI线程上执行(换句话说,它抽象处理程序■从开发者)。

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

这篇关于线程/可运行,处理程序,runonuithread,AsyncTask的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆