android runOnUiThread和Java中的简单代码之间的区别 [英] Difference between android runOnUiThread and simple code in java

查看:121
本文介绍了android runOnUiThread和Java中的简单代码之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android应用程序开发的初学者.我正在android中使用线程.我已经读过一个runOnUiThread可以在主UI上运行代码(如果我没记错的话?).

I am a beginner in android application development.I am working with threads in android.I have read about a runOnUiThread which run code on main UI(if i am not wrong?i guess.).

我的问题是主UI上的普通代码与runOnIUThread中的代码有什么区别.

My question is what is the difference between normal code on main UI and code inside runOnIUThread.

示例:1

class A
{
getDataFromServer(foo);//Code on mainUI
}

示例:2

getActivity.runOnUiThread(new Runnable(){
@Override
public void run(){
getDataFromServer(foo);
}
});

这两个示例有什么不同.请帮助我.您的回复对我来说将是一种新的学习.

What is difference in both example.Please help me.Your response will be a new learning for me.

推荐答案

假设您要使用的是UIThread代码的简单代码,

Assuming that you meant simple code for UIThread code,

什么是线程?

一个线程定义了一个正在运行的进程

A thread defines a process running

第一个 runOnUiThread ..

UI线程上运行指定的操作. 如果当前线程是 UI线程,然后立即执行操作.如果当前 线程不是UI线程不是,该操作将发布到事件队列中 UI线程.

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

什么是 UIThread

  • 您的应用程序的执行主线程
  • 大多数应用程序代码将在此处运行onCreateonPauseonDestroyonClick等.

所以简单地任何会导致UI更新或更改的事件都将在UI线程上发生

显式生成一个新线程以在后台中工作时,该代码不会在UIThread上运行.用户界面? 那么欢迎您来到runOnUiThread

When you explicitly spawn a new thread to do work in the background, this code is not run on the UIThread.Now what if you want to do something that changes the UI? Then you are welcome to runOnUiThread

当您要从非UI线程更新UI时,必须使用runOnUiThread().例如-如果您想从后台线程更新您的UI.您也可以将Handler用于同一件事.

You have to use runOnUiThread() when you want to update your UI from a Non-UI Thread. For eg- If you want to update your UI from a background Thread. You can also use Handler for the same thing.

这篇关于android runOnUiThread和Java中的简单代码之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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