Android的延迟实现的几种解决方案以及原理分析

写这篇文章的目的,是看到群里有人在实现延迟的时候,用如下的第四种方法,个人感觉有点不妥,为了防止更多的人有这种想法,所以自己抽空深入分析,就分析的结果,写下此文,希望对部分人有启示作用。

1.实现延迟的几种方法?

答: 1.java.util.Timer类的:

public void schedule(TimerTask task, long delay) {
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        sched(task, System.currentTimeMillis()+delay, 0);
    }

2.android.os.Handler类:

public final boolean postDelayed(Runnable r, long delayMillis)
    {
        return sendMessageDelayed(getPostMessage(r), delayMillis);
    }

3.android.app.AlarmManager类:

@SystemApi
    @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
    public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
            long intervalMillis, OnAlarmListener listener, Handler targetHandler,
            WorkSource workSource) {
        setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null,
                targetHandler, workSource, null);
    }

4.Thread.sleep()然后在一定时间之后再执行想执行的代码:

new Thread(new Runnable(){
    Thead.sleep(4*1000);
    doTask();
}).start()

2.他们的各自的实现原理?

答:

1.Timer的实现,是通过内部开启一个TimerThread:

private void mainLoop() {
        while (true) {
            try {
                TimerTask task;
                boolean taskFired;
                synchronized(queue) {
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && newTasksMayBeScheduled)
                        queue.wait();
                    if (queue.isEmpty())
                        break; // Queue is empty and will forever remain; die

                    // Queue nonempty; look at first evt and do the right thing
                    long currentTime, executionTime;
                    task = queue.getMin();
                    synchronized(task.lock) {
                        if (task.state == TimerTask.CANCELLED) {
                            queue.removeMin();
                            continue;  // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis();
                        executionTime = task.nextExecutionTime;
                        if (taskFired = (executionTime<=currenttime)) {="" if="" (task.period="=" 0)="" non-repeating,="" remove="" queue.removemin();="" task.state="TimerTask.EXECUTED;" }="" else="" repeating="" task,="" reschedule="" queue.reschedulemin(="" task.period<0="" ?="" currenttime="" -="" task.period="" :="" executiontime="" +="" task.period);="" (!taskfired)="" task="" hasn't="" yet="" fired;="" wait="" queue.wait(executiontime="" currenttime);="" (taskfired)="" run="" it,="" holding="" no="" locks="" task.run();="" catch(interruptedexception="" e)="" 
  

是通过wait和延迟时间到达的时候,调用notify来唤起线程继续执行,这样来实现延迟的话,我们可以回开启一个新的线程,貌似为了个延迟没必要这样吧,定时,频繁执行的任务,再考虑这个吧。

2.Handler的postDelay是通过设置Message的when为delay的时间,我们知道当我们的应用开启的时候,会同步开启Looper.loop()方法循环的,不停的通过MeassgeQueue的next方法:

Message next() {
        ......
        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.
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }
                if (msg != null) {
                    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.
                        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;
                }
        ......
        }
    }

当我们向MessageQueue插入一条延迟的Message的时候,Looper在执行loop方法,底层会调用epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);其中的timeoutMillis参数指定了在没有事件发生的时候epoll_wait调用阻塞的毫秒数(milliseconds)。这样我们在之前的时间内这个时候阻塞了是会释放cpu的资源,等到延迟的时间到了时候,再监控到事件发生。在这里可能有人会有疑问,一直阻塞,那我接下来的消息应该怎么执行呢?我们可以看到当我们插入消息的时候的方法:

boolean enqueueMessage(Message msg, long when) {
        if (msg.target == null) {
            throw new IllegalArgumentException("Message must have a target.");
        }
        if (msg.isInUse()) {
            throw new IllegalStateException(msg + " This message is already in use.");
        }

        synchronized (this) {
            if (mQuitting) {
                IllegalStateException e = new IllegalStateException(
                        msg.target + " sending message to a Handler on a dead thread");
                Log.w(TAG, e.getMessage(), e);
                msg.recycle();
                return false;
            }

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            if (p == null || when == 0 || when < p.when) {
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }
            mQuitting is false.
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }

阻塞了有两种方式唤醒,一种是超时了,一种是被主动唤醒了,在上面我们可以看到当有消息进入的时候,我们会唤醒继续执行,所以我们的即时消息在延迟消息之后插入是没有关系的。然后在延迟时间到了的时候,我们也会被唤醒,执行对应的消息send,以达到延迟时间执行某个任务的目的。 优势:这种延迟在阻塞的时候,是会释放cpu的锁,不会过多地占用cpu的资源。

3.AlarmManager的延迟的实现原理,是通过一个AlarmManager的set方法,然后

IAlarmManager mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags,
                    operation, recipientWrapper, listenerTag, workSource, alarmClock);

这里是通过aidl与AlarmManagerService的所在进程进行通信,具体的实现是在AlarmManagerService类里面:

private final IBinder mService = new IAlarmManager.Stub() {
        @Override
        public void set(String callingPackage,
                int type, long triggerAtTime, long windowLength, long interval, int flags,
                PendingIntent operation, IAlarmListener directReceiver, String listenerTag,
                WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock) {
            final int callingUid = Binder.getCallingUid();
            if (interval != 0) {
                if (directReceiver != null) {
                    throw new IllegalArgumentException("Repeating alarms cannot use AlarmReceivers");
                }
            }

            if (workSource != null) {
                getContext().enforcePermission(
                        android.Manifest.permission.UPDATE_DEVICE_STATS,
                        Binder.getCallingPid(), callingUid, "AlarmManager.set");
            }

            // No incoming callers can request either WAKE_FROM_IDLE or
            // ALLOW_WHILE_IDLE_UNRESTRICTED -- we will apply those later as appropriate.
            flags &= ~(AlarmManager.FLAG_WAKE_FROM_IDLE
                    | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED);

            // Only the system can use FLAG_IDLE_UNTIL -- this is used to tell the alarm
            // manager when to come out of idle mode, which is only for DeviceIdleController.
            if (callingUid != Process.SYSTEM_UID) {
                flags &= ~AlarmManager.FLAG_IDLE_UNTIL;
            }

            if (windowLength == AlarmManager.WINDOW_EXACT) {
                flags |= AlarmManager.FLAG_STANDALONE;
            }
            if (alarmClock != null) {
                flags |= AlarmManager.FLAG_WAKE_FROM_IDLE | AlarmManager.FLAG_STANDALONE;
            } else if (workSource == null && (callingUid = 0)) {
                flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED;
                flags &= ~AlarmManager.FLAG_ALLOW_WHILE_IDLE;
            }

            setImpl(type, triggerAtTime, windowLength, interval, operation, directReceiver,
                    listenerTag, flags, workSource, alarmClock, callingUid, callingPackage);
        }
    }
}

虽然有人觉得用AlarmManager能够在应用关闭的情况下,定时器还能再唤起,经过自己的测试,当杀掉应用程序的进程,AlarmManager的receiver也是接收不到消息的,但是我相信在这里定时器肯定是发送了,但是作为接收方的应用程序进程被杀掉了,执行不了对应的代码。不过有人也觉得AlarmManager更耗电,是因为我们执行定时任务的情况会频繁唤起cpu,但是如果只是用来只是执行延迟任务的话,个人觉得和Handler.postDelayed()相比应该也不会耗电多的。

2.在上面的第四种方法,达到的延迟会一直通过Thread.sleep来达到延迟的话,会一直占用cpu的资源,这种方法不赞同使用。

3.总结

如上面我们看到的这样,如果是单纯的实现一个任务的延迟的话,我们可以用Handler.postDelayed()和AlarmManager.set()来实现,用(4)的方法Thread.sleep()的话,首先开启一个新的线程,然后会持有cpu的资源,用(1)的方法,Timer,会开启一个死循环的线程,这样在资源上面都有点浪费。

如果大家还有更好的延迟解决方案,可以拿出来大家探讨,如果文章有不对的地方,欢迎拍砖。

稀土掘金稿源:稀土掘金 (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Android的延迟实现的几种解决方案以及原理分析

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录