handler.post(runnable) run in child thread ,but why it can update ui thread?

73 Views Asked by At

this is my code

HandlerThread handlerThread = new HandlerThread("test");
        handlerThread.start();
Handler handler_test = new Handler(handlerThread.getLooper());
handler_test.post(()->{
            Log.d("handlerr_test", String.valueOf(Looper.getMainLooper()==Looper.myLooper()));
            surfaceView.setVisibility(View.GONE);
            Log.d("handlerr_test", String.valueOf(Looper.getMainLooper()==Looper.myLooper()));
        });

this code logged "false", but surfaceView is gone successfully. the code in handler_test.post() should run in child thread test,but whysurfaceView.setVisibility(View.GONE)could update ui thread widget successfully?

NEW found: now i found that if i replace handler_test.post(run) with handler_test.postDelayed(run,50) or handler_test.postDelayed(run,100),this code would fail updating the ui widget surfaceView. But with low delay,such as 10ms, handler_test.postDelayed(run,10),this code would still update the ui widget successfully.

However,in any case,this code logged "false",showed that this code run in child thread.So why this code could update ui thred after low delay but not after high delay?

0

There are 0 best solutions below