Android IdleHandler闲时加载

2024-01-01 13:06:39

在之前的文章里,我们讲过关于handler的一些使用和原理。

Android Handler全使用

Android Handler原理源码全解析

今天讲一个系统预留的一个handler,IdleHandler,有了它,可以让我们在系统闲时进行一些预加载或者事务处理。

使用方式

Looper.myQueue().addIdleHandler(new IdleHandler() {

    @Override
    public boolean queueIdle() {
        //具体的业务逻辑
        return false;
    }
}

需要注意的是,这里的返回值,可以为true也可以为false。

区别在于,return false代表该闲时任务仅执行一次。return true代表只要系统闲,就会反复执行多次。

原理

在MessageQueue.java中,有idleHandler的添加与移除

/**
 * Callback interface for discovering when a thread is going to block
 * waiting for more messages.
 */
public static interface IdleHandler {
    /**
     * Called when the message queue has run out of messages and will now
     * wait for more.  Return true to keep your idle handler active, false
     * to have it removed.  This may be called if there are still messages
     * pending in the queue, but they are all scheduled to be dispatched
     * after the current time.
     */
    boolean queueIdle();
}

/**
 * Add a new {@link IdleHandler} to this message queue.  This may be
 * removed automatically for you by returning false from
 * {@link IdleHandler#queueIdle IdleHandler.queueIdle()} when it is
 * invoked, or explicitly removing it with {@link #removeIdleHandler}.
 *
 * <p>This method is safe to call from any thread.
 *
 * @param handler The IdleHandler to be added.
 */
public void addIdleHandler(@NonNull IdleHandler handler) {
    if (handler == null) {
        throw new NullPointerException("Can't add a null IdleHandler");
    }
    synchronized (this) {
        mIdleHandlers.add(handler);
    }
}

/**
 * Remove an {@link IdleHandler} from the queue that was previously added
 * with {@link #addIdleHandler}.  If the given object is not currently
 * in the idle list, nothing is done.
 *
 * <p>This method is safe to call from any thread.
 *
 * @param handler The IdleHandler to be removed.
 */
public void removeIdleHandler(@NonNull IdleHandler handler) {
    synchronized (this) {
        mIdleHandlers.remove(handler);
    }
}

在MessageQueue的next方法中

Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after
// quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
    return null;
}

int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
    if (nextPollTimeoutMillis != 0) {
        Binder.flushPendingCommands();
    }

    nativePollOnce(ptr, nextPollTimeoutMillis);

    synchronized (this) {
        // Try to retrieve the next message. Return if found.
        final long now = SystemClock.uptimeMillis();
        Message prevMsg = null;
        Message msg = mMessages;
        if (msg != null && msg.target == null) {
            // Stalled by a barrier. Find the next asynchronous message
            // in the queue.
            // 这里执行的操作是忽略所有的同步消息, 知道找出queue中的异步消息
            // 我理解是这个的同步消息会造成线程的阻塞, 所以忽略同步的消息
            do {
                prevMsg = msg;
                msg = msg.next;
            } while (msg != null && !msg.isAsynchronous());
        }
        // 走到这一步, 有两种可能,
        // 一种是遍历到队尾没有发现异步消息,
        // 另一种是找到queue中的第一个异步消息
        if (msg != null) {
            // 找到queue中的第一个异步消息
            if (now < msg.when) {
                // Next message is not ready. Set a timeout to wake up
                // when it is ready.
                // 没有到消息的执行时间
                nextPollTimeoutMillis = (int) Math.min(msg.when - now,
                        Integer.MAX_VALUE);
            } else {
                // Got a message.
                // 当前消息到达可以执行的时间, 直接返回这个msg
                mBlocked = false;
                if (prevMsg != null) {
                    prevMsg.next = msg.next;
                } else {
                    mMessages = msg.next;
                }
                msg.next = null;
                if (DEBUG)
                    Log.v(TAG, "Returning message: " + msg);
                msg.markInUse();
                return msg;
            }
        } else {
            // 遍历到队尾, 没有发现异步消息或者没有消息了
            // No more messages.
            nextPollTimeoutMillis = -1;
        }

        // Process the quit message now that all pending messages have
        // been handled.
        // 检查当前的线程是否退出
        if (mQuitting) {
            dispose();
            return null;
        }

        // If first time idle, then get the number of idlers to run.
        // Idle handles only run if the queue is empty or if the first
        // message
        // in the queue (possibly a barrier) is due to be handled in the
        // future.
        // 如果queue中没有msg, 或者msg没到可执行的时间,
        // 那么现在线程就处于空闲时间了, 可以执行IdleHandler了
        if (pendingIdleHandlerCount < 0
                && (mMessages == null || now < mMessages.when)) {
            // pendingIdleHandlerCount在进入for循环之前是被初始化为-1的
            // 并且没有更多地消息要进行处理
            pendingIdleHandlerCount = mIdleHandlers.size();
        }
        if (pendingIdleHandlerCount <= 0) {
            // No idle handlers to run. Loop and wait some more.
            // 如果没有IdleHandler要进行处理, 则直接进入下次循环
            mBlocked = true;
            continue;
        }

        if (mPendingIdleHandlers == null) {
            mPendingIdleHandlers = new IdleHandler[Math.max(
                    pendingIdleHandlerCount, 4)];
        }
        mPendingIdleHandlers = mIdleHandlers
                .toArray(mPendingIdleHandlers);
    }

    // Run the idle handlers.
    // We only ever reach this code block during the first iteration.
    // 退出同步块, 接下来就可以执行IdleHandler的相关操作了
    for (int i = 0; i < pendingIdleHandlerCount; i++) {
        final IdleHandler idler = mPendingIdleHandlers[i];
        mPendingIdleHandlers[i] = null; // release the reference to the
                                        // handler

        boolean keep = false;
        try {
            keep = idler.queueIdle();
        } catch (Throwable t) {
            Log.wtf(TAG, "IdleHandler threw exception", t);
        }

        if (!keep) {
            // 如果之前addIdleHandler中返回为false,
            // 就在执行完这个IdleHandler的callback之后, 将这个idler移除掉
            synchronized (this) {
                mIdleHandlers.remove(idler);
            }
        }
    }

    // Reset the idle handler count to 0 so we do not run them again.
    // 全部执行完, 重新设置这个值为0, 以便下次可以再次执行
    pendingIdleHandlerCount = 0;

    // While calling an idle handler, a new message could have been
    // delivered
    // so go back and look again for a pending message without waiting.
    nextPollTimeoutMillis = 0;
}
}

从代码,可以看出,判断闲时的逻辑,即可以处理idleHandler的时机是:

如果queue中没有msg, 或者msg没到可执行的时间, 此时线程就处于空闲时间了,可以执行IdleHandler了

文章来源:https://blog.csdn.net/yaojie5519/article/details/129995415
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。