阅读 20

View.Post

示例

问题:为什么log日志会不打印。

public class MainThreadTestActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_thread_test);

    View view = new View(this);
    view.post(new Runnable() {
      @Override
      public void run() {
        Log.i(TAG, "[view.post] >>>> 1 ");
      }
    });
  }
}
复制代码

源码分析

//View.java
public boolean post(Runnable action) {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler.post(action);
    }

    // Postpone the runnable until we know on which thread it needs to run.
    // Assume that the runnable will be successfully placed after attach.
    getRunQueue().post(action);
    return true;
}
复制代码

即:当执行 View.post 方法时,如果 AttachInfo 不为空,则通过 AttachInfoHandler 来执行 Runnable;否则,将这个 Runnable 抛到 View 的执行队列 HandlerActionQueue 中。

void dispatchAttachedToWindow(AttachInfo info, int visibility) {
        mAttachInfo = info;
        //...
}
复制代码

也就是只有当 View attch 到 Window 后,才会给 AttachInfo 赋值。所以,在 示例 里的代码会直接走入 getRunQueue().post(action) 。

public class HandlerActionQueue {
    private HandlerAction[] mActions;
    private int mCount;

    public void post(Runnable action) {
        postDelayed(action, 0);
    }

    public void postDelayed(Runnable action, long delayMillis) {
        final HandlerAction handlerAction = new HandlerAction(action, delayMillis);

        synchronized (this) {
            if (mActions == null) {
                mActions = new HandlerAction[4];
            }
            mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
            mCount++;
        }
    }

    public void removeCallbacks(Runnable action) {
        synchronized (this) {
            final int count = mCount;
            int j = 0;

            final HandlerAction[] actions = mActions;
            for (int i = 0; i < count; i++) {
                if (actions[i].matches(action)) {
                    // Remove this action by overwriting it within
                    // this loop or nulling it out later.
                    continue;
                }

                if (j != i) {
                    // At least one previous entry was removed, so
                    // this one needs to move to the "new" list.
                    actions[j] = actions[i];
                }

                j++;
            }

            // The "new" list only has j entries.
            mCount = j;

            // Null out any remaining entries.
            for (; j < count; j++) {
                actions[j] = null;
            }
        }
    }

    public void executeActions(Handler handler) {
        synchronized (this) {
            final HandlerAction[] actions = mActions;
            for (int i = 0, count = mCount; i < count; i++) {
                final HandlerAction handlerAction = actions[i];
                handler.postDelayed(handlerAction.action, handlerAction.delay);
            }

            mActions = null;
            mCount = 0;
        }
    }
}
复制代码

而在 View 中,

//View.java
void dispatchAttachedToWindow(AttachInfo info, int visibility) {
       ...
        // Transfer all pending runnables.
        if (mRunQueue != null) {
            mRunQueue.executeActions(info.mHandler);
            mRunQueue = null;
        }
        ...
}


//ViewRootImpl.java
private void performTraversals() {
    ...
    getRunQueue().executeActions(mAttachInfo.mHandler);
    ...
}
复制代码

由于 View view = new View(this); 没有将view attch到window上,所以执行的 View.post 方法将可执行请求都缓存到请求队列里。

示例 中的代码可改为:

View view = new View(this);
rootView.addView(view);
view.post(new Runnable() {
复制代码

参考:

runOnUiThread 、Handler.post、View.post分析

文章分类
Android
文章标签