zoukankan      html  css  js  c++  java
  • 【朝花夕拾】Android多线程之(三)runOnUiThread篇——程序猿们的贴心小棉袄

           runOnUiThread()的使用以及原理实在是太简单了,简单到笔者开始都懒得单独开一篇文章来写它。当然这里说的简单,是针对对Handler比较熟悉的童鞋而言的。不过麻雀虽小,五脏俱全,runOnUiThread()好歹也算得上是一方诸侯,在子线程切换到主线程的众多方法中,有着自己的一席之地,所以,必须得给它单独列传。

           好了,闲话休提,言归正传。runOnUiThread()是Activity类中的方法,它用于从子线程中切换到主线程来执行一些需要再主线程执行的操作。这里先直接看一个例子,看看它是如何使用的:

     1 public class MainActivity extends AppCompatActivity {
     2     private TextView textView;
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7         textView = findViewById(R.id.tv_test);
     8         new Thread(new Runnable() {
     9             @Override
    10             public void run() {
    11                 //do something takes long time in the work-thread
    12                 runOnUiThread(new Runnable() {
    13                     @Override
    14                     public void run() {
    15                         textView.setText("test");
    16                     }
    17                 });
    18             }
    19         }).start();
    20     }
    21 }

    简单吧,在子线程中直接调用runOnUiThread方法,第15行就切换到主线程了,直接修改UI。如果使用Lambda表达式,看起来就更简单了:

    1 new Thread(() -> {
    2     //do something takes long time in the work-thread
    3     runOnUiThread(() -> {
    4         textView.setText("test");
    5     });
    6 }).start();

    相比于通过显示使用Handler,重写AsyncTask方法来说,是不是爽得不要不要的?

           不仅仅使用简单,其原理也非常简单,底层实际上也是封装的Handler来实现的,如下是关键代码:

     1 //=========Activity=========
     2 final Handler mHandler = new Handler();
     3 
     4 private Thread mUiThread;
     5 
     6 final void attach(...){
     7     ......
     8     mUiThread = Thread.currentThread();
     9     ......
    10 }
    11 
    12 /**
    13  * Runs the specified action on the UI thread. If the current thread is the UI
    14  * thread, then the action is executed immediately. If the current thread is
    15  * not the UI thread, the action is posted to the event queue of the UI thread.
    16  *
    17  * @param action the action to run on the UI thread
    18  */
    19 public final void runOnUiThread(Runnable action) {
    20     if (Thread.currentThread() != mUiThread) {
    21         mHandler.post(action);
    22     } else {
    23         action.run();
    24     }
    25 }

    mHander是Activity的成员变量,在Activity实例化的时候也跟着初始化了,MainActivity继承自Activity,这里mHandler使用的looper自然是main looper了。attach方法也是在主线程中调用的,mUiThread就表示主线程了。第19行的方法就很容易理解了,如果该方法是运行在主线程,Runnable的run方法会马上运行;而如果不是在主线程,就post到主线程的looper的MessageQueue中排队执行。

           基本使用和基本原理就讲完了,够简单吧,也确实没多少重要的东西可讲的了!真不愧是广大程序猿们的贴心小棉袄,要是Android的各个方法都这么简单,想必就没有那么多秃顶了!

           好了,洗澡睡觉!

  • 相关阅读:
    Nginx会话保持之nginx-sticky-module模块
    企业级分布式应用服务EDAS _Dubbo商业版_微服务PaaS平台 【EDAS Serverless 运维 创业】
    git repository description
    运维成长
    jenkins+maven+tomcat集群发布
    Leaf——美团点评分布式ID生成系统 UUID & 类snowflake
    tomcat redis 集群 session共享
    JEECG & JEESite Tomcat集群 Session共享
    分布式Tomcat session会话Sticky Sessions问题
    Memcached 集群架构与memcached-session-manager
  • 原文地址:https://www.cnblogs.com/andy-songwei/p/12064596.html
Copyright © 2011-2022 走看看