HandlerThread brief explanation

    Anyone who has been exposed to Android project development should know that Handler plays an important role in Android. Because of the development of a project, the use of multi-threading is essential, but what if you want to operate the UI after the asynchronous thread completes some time-consuming operations, because the Android design does not allow the UI to be updated in the child thread? ?
    Using Handler, a Message is sent in the past, and this operation can be easily completed.
    For the application of Handler, most scenarios are like this. But what if you want to implement asynchronous operations in the Handler's Message method? Some ignorant friends may say, just use Handler.post(new Runnable(){}); just fine, if you still have this idea, it is recommended that you go back and deepen your understanding of Handler, Thread, and Runnable. Of course, some friends will say that if you create a new Thread in the handlerMessage() method of Handler, you will be done. Indeed, there is no problem with creating a new Thread to implement asynchronous operations, but if the Message sent by the Handler has the necessary order of execution, if a new thread is opened every time a Message is received, then the application's Thread management will be extremely complicated. If you are not careful, various problems may occur.
In view of the above situations, your savior is here, it is --HandlerThread! ! !
For HandlerThread, it is explained in the API like this:

    Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

     HandlerThread is a very convenient class, it is used to start a new thread with Looper, and this Looper is suitable for creating Handler objects. But it has a usage condition, that is, the start() method must be executed first

    private HandlerThread ht;
    private Handler mThreadHandler;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity = this;

        //init views
        mThreadNameView = findViewById(R.id.thread_name);
        mUiHandlerBtn = findViewById(R.id.ui_handler_btn);
        mHandlerThreadBtn = findViewById(R.id.thread_handler_btn);
        mUiHandlerBtn.setOnClickListener(this);
        mHandlerThreadBtn.setOnClickListener(this);

        //init Handler
        ht = new HandlerThread("Handler Thread");
        ht.start();
        mThreadHandler = new Handler(ht.getLooper());
    }
    @SuppressLint("HandlerLeak")
    private Handler mUIHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.i(TAG, "mUIHandler: current thread name: " + Thread.currentThread().getName());
            Log.i(TAG, "mUIHandler: current thread is main thread ? "
                    + (Thread.currentThread().getId() == Looper.getMainLooper().getThread().getId()));
            showCurrentThreadName(Thread.currentThread());
        }
    };

    private void showCurrentThreadName(final Thread myThread) {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mThreadNameView.setText(myThread.getName());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ui_handler_btn:
                mUIHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "mUIHandler run on new Runnable, current thread is "
                                + Thread.currentThread().getName());
                    }
                });
                mUIHandler.sendEmptyMessage(0);
                break;
            case R.id.thread_handler_btn:
                mThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        showCurrentThreadName(Thread.currentThread());
                    }
                });
                break;
        }
    }

    Through execution, it can be found that the UIHandler created in the main thread, in handlerMessage or new Runnable(){}, is executed in the main thread, while the ThreadHandler created by the Looper of HandlerThread runs in the "Handler Thread" thread middle.

So far, the simple explanation and application of HandlerThread is basically completed.

Attach my Demo link here https://gitee.com/meinkonone/HanderThread

 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326307125&siteId=291194637