Four Common Ways of Communication Between Threads in android

  • 2021-12-12 09:50:53
  • OfStack

1. Through Handler mechanism

Handler is defined in the main thread, and the sub-thread sends a message to inform Handler to complete UI update. Handler objects must be defined in the main thread. If multiple classes call each other directly, it is not very convenient, and content objects need to be passed or called through interfaces. In addition, Handler mechanism and Activity life cycle is not the same reason, which easily leads to memory leakage, so it is not recommended to use.


private void one() {
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 123:
                        tv.setText(""+msg.obj);
                        break;
                }
            }
        };
        new Thread(){
            @Override
            public void run() {
                super.run();
                for (int i=0;i<3;i++){
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Message message=new Message();
                message.what=123;
                message.obj=" Pass Handler Mechanism ";
                handler.sendMessage(message);
            }
        }.run();
    }

2. runOnUiThread method


private void two(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                for (int i=0;i<3;i++){
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText(" Pass runOnUiThread Method ");
                    }
                });
            }
        }.run();
    }

Update with the runOnUiThread method of the Activity object, and update UI with the runOnUiThread () method in child threads, highly recommended.

3. View. post (Runnable r),


private void three(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                for (int i=0;i<3;i++){
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                tv.post(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText(" Pass View.post(Runnable r)  Method ");
                    }
                });
            }
        }.run();
    }

This method is simpler, but it needs to pass the View to be updated. It is recommended to use

4. AsyncTask


private void four(){
        new MyAsyncTask().execute(" Pass AsyncTask Method ");
    }
private class MyAsyncTask extends AsyncTask{
        @Override
        protected Object doInBackground(Object[] objects) {
            for (int i=0;i<3;i++){
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return objects[0].toString();
        }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            tv.setText(o.toString());
        }
    }

Related articles: