Will Runnables block the UI thread?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 08:15:54

Well, Runnable inside your runOnUiThread is just operation in Main Thread.

Imagine that some simple action like

textView.setText("example");

will block Main Thread for 5 ms. Usually you will not see it.

Now imagine that you have like 1000 same operations for 5 seconds. And every blocks Main Thread for 5 ms. Simple calculating 5ms * 1000 = 5000ms = 5 seconds. So it will block Main Thread permamently. But if you have 10 operations you will block only 50 ms, in other words its just 1% of load that you will not feel.

So possible amount of calls depends on size of View, how hard render is and how fast is device.

P.S. For adepts of AsyncTask - there is no real difference between runOnUiThread and AsyncTask because those 1000 Runnables will execute in Main Thread by the same way.

Even if I do same thing inside onCreate method of Activity that will block UI hard

Yes. Runnable executing on UI thread will block main thread.

Check if below approach is useful for you.

  1. Create a Handler with Looper from Main :requestHandler
  2. Create a Handler with Looper for main thread : responseHandler and override handleMessage method
  3. post a Runnable task on requestHandler
  4. Inside Runnable task, call sendMessage on responseHandler
  5. This sendMessage result invocation of handleMessage in responseHandler.
  6. Get attributes from the Message and process it, update UI

Sample code:

    /* Handler */

    Handler requestHandler = new Handler(Looper.getMainLooper());

    final Handler responseHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            //txtView.setText((String) msg.obj);
            Toast.makeText(MainActivity.this,
                    "Adwork task is completed:"+(String)msg.obj,
                    Toast.LENGTH_LONG)
                    .show();
        }
    };

    for ( int i=0; i<10; i++) {
        // Start Adwork task
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    /* Your business logic goes here */
                    // Send some result after computation
                    String text = "" + (++rId);
                    Message msg = new Message();

                    msg.obj = text.toString();
                    responseHandler.sendMessage(msg);
                    System.out.println(text.toString());

                } catch (Exception err) {
                    err.printStackTrace();
                }
            }
        };
        requestHandler.post(myRunnable);
    }

Useful articles:

handlerthreads-and-why-you-should-be-using-them-in-your-android-apps

android-looper-handler-handlerthread-i

What you are looking for is an AsyncTask.

These are designed to do some background processing, while the UI thread continues. It will NOT block the UI and will NOT cause ANR.

Within the AsyncTask, is an onPostExecute method, which allows you to post results back to the UI. So it is not completely detached from the UI Thread. And an onProgressUpdate for connection during the background processing

Yes, you will feel animation will stop or start shutter if You run heavy operations in Ui thread. It's also depends of how fast device is.

What I would suggest you is to break your code in two parts. One that can be done in background and second that need Ui thread and execute them in AsyncTask.

Use doInBackgroud method to execute heavy operation and onPostExecute to update UI.

Note: If you break code into 1000 threads and run them in one moment, then probably You will hit device queue limit. (Break them into 10-50 parts. )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!