【朝花夕拾】Android多線程之(三)runOnUiThread篇——程序猿們的貼心小棉襖

       runOnUiThread()的使用以及原理實在是太簡單了,簡單到筆者開始都懶得單獨開一篇文章來寫它。固然這裏說的簡單,是針對對Handler比較熟悉的童鞋而言的。不過麻雀雖小,五臟俱全,runOnUiThread()好歹也算得上是一方諸侯,在子線程切換到主線程的衆多方法中,有着本身的一席之地,因此,必須得給它單獨列傳。ide

       好了,閒話休提,言歸正傳。runOnUiThread()是Activity類中的方法,它用於從子線程中切換到主線程來執行一些須要再主線程執行的操做。這裏先直接看一個例子,看看它是如何使用的:oop

 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                 MainActivity.this.runOnUiThread(new Runnable() {
13                     @Override
14                     public void run() {
15                         textView.setText("test");
16                     }
17                 });
18             }
19         });
20     }
21 }

簡單吧,在子線程中直接調用runOnUiThread方法,第15行就切換到主線程了,直接修改UI。若是使用Lambda表達式,看起來就更簡單了:post

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

相比於經過顯示使用Handler,重寫AsyncTask方法來講,是否是爽得不要不要的?this

       不單單使用簡單,其原理也很是簡單,底層實際上也是封裝的Handler來實現的,以下是關鍵代碼:spa

 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的各個方法都這麼簡單,想必就沒有那麼多禿頂了!code

       好了,洗澡睡覺!blog

相關文章
相關標籤/搜索