欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android延遲實(shí)現(xiàn)的幾種解決方法及原理分析

 更新時(shí)間:2017年12月08日 09:19:45   作者:wellijhon  
這篇文章主要給大家介紹了關(guān)于Android延遲實(shí)現(xiàn)的幾種解決方法以及其中的原理分析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

在Android開發(fā)中我們可能會(huì)有延時(shí)執(zhí)行某個(gè)操作的需求,例如我們啟動(dòng)應(yīng)用的時(shí)候,一開始呈現(xiàn)的是一個(gè)引導(dǎo)頁(yè)面,過了兩三秒后,會(huì)自動(dòng)跳轉(zhuǎn)到主界面。這就是一個(gè)延時(shí)操作。

而寫這篇文章的目的,是看到群里有人在實(shí)現(xiàn)延遲的時(shí)候,用如下的第四種方法,個(gè)人感覺有點(diǎn)不妥,為了防止更多的人有這種想法,所以自己抽空深入分析,就分析的結(jié)果,寫下此文,希望對(duì)部分人有啟示作用。

1.實(shí)現(xiàn)延遲的幾種方法?

答:

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()然后在一定時(shí)間之后再執(zhí)行想執(zhí)行的代碼:

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

2.他們的各自的實(shí)現(xiàn)原理?

答:

1.Timer的實(shí)現(xiàn),是通過內(nèi)部開啟一個(gè)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);
    }
   }
   }
   if (!taskFired) // Task hasn't yet fired; wait
   queue.wait(executionTime - currentTime);
  }
  if (taskFired) // Task fired; run it, holding no locks
   task.run();
  } catch(InterruptedException e) {
  }
 }
 }

是通過wait和延遲時(shí)間到達(dá)的時(shí)候,調(diào)用notify來喚起線程繼續(xù)執(zhí)行,這樣來實(shí)現(xiàn)延遲的話,我們可以回開啟一個(gè)新的線程,貌似為了個(gè)延遲沒必要這樣吧,定時(shí),頻繁執(zhí)行的任務(wù),再考慮這個(gè)吧。

2.Handler的postDelay是通過設(shè)置Message的when為delay的時(shí)間,我們知道當(dāng)我們的應(yīng)用開啟的時(shí)候,會(huì)同步開啟Looper.loop()方法循環(huán)的,不停的通過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;
    }
  ......
  }
 }

當(dāng)我們向MessageQueue插入一條延遲的Message的時(shí)候,Looper在執(zhí)行l(wèi)oop方法,底層會(huì)調(diào)用epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);其中的timeoutMillis參數(shù)指定了在沒有事件發(fā)生的時(shí)候epoll_wait調(diào)用阻塞的毫秒數(shù)(milliseconds)。這樣我們?cè)谥暗臅r(shí)間內(nèi)這個(gè)時(shí)候阻塞了是會(huì)釋放cpu的資源,等到延遲的時(shí)間到了時(shí)候,再監(jiān)控到事件發(fā)生。在這里可能有人會(huì)有疑問,一直阻塞,那我接下來的消息應(yīng)該怎么執(zhí)行呢?

我們可以看到當(dāng)我們插入消息的時(shí)候的方法:

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;
 }

阻塞了有兩種方式喚醒,一種是超時(shí)了,一種是被主動(dòng)喚醒了,在上面我們可以看到當(dāng)有消息進(jìn)入的時(shí)候,我們會(huì)喚醒繼續(xù)執(zhí)行,所以我們的即時(shí)消息在延遲消息之后插入是沒有關(guān)系的。然后在延遲時(shí)間到了的時(shí)候,我們也會(huì)被喚醒,執(zhí)行對(duì)應(yīng)的消息send,以達(dá)到延遲時(shí)間執(zhí)行某個(gè)任務(wù)的目的。

優(yōu)勢(shì):這種延遲在阻塞的時(shí)候,是會(huì)釋放cpu的鎖,不會(huì)過多地占用cpu的資源。

3.AlarmManager的延遲的實(shí)現(xiàn)原理,是通過一個(gè)AlarmManager的set方法:

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

這里是通過aidl與AlarmManagerService的所在進(jìn)程進(jìn)行通信,具體的實(shí)現(xiàn)是在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 < Process.FIRST_APPLICATION_UID
     || Arrays.binarySearch(mDeviceIdleUserWhitelist,
       UserHandle.getAppId(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能夠在應(yīng)用關(guān)閉的情況下,定時(shí)器還能再喚起,經(jīng)過自己的測(cè)試,當(dāng)殺掉應(yīng)用程序的進(jìn)程,AlarmManager的receiver也是接收不到消息的,但是我相信在這里定時(shí)器肯定是發(fā)送了,但是作為接收方的應(yīng)用程序進(jìn)程被殺掉了,執(zhí)行不了對(duì)應(yīng)的代碼。不過有人也覺得AlarmManager更耗電,是因?yàn)槲覀儓?zhí)行定時(shí)任務(wù)的情況會(huì)頻繁喚起cpu,但是如果只是用來只是執(zhí)行延遲任務(wù)的話,個(gè)人覺得和Handler.postDelayed()相比應(yīng)該也不會(huì)耗電多的。

2.在上面的第四種方法,達(dá)到的延遲會(huì)一直通過Thread.sleep來達(dá)到延遲的話,會(huì)一直占用cpu的資源,這種方法不贊同使用。

3.總結(jié)

如上面我們看到的這樣,如果是單純的實(shí)現(xiàn)一個(gè)任務(wù)的延遲的話,我們可以用Handler.postDelayed()AlarmManager.set()來實(shí)現(xiàn),用(4)的方法Thread.sleep()的話,首先開啟一個(gè)新的線程,然后會(huì)持有cpu的資源,用(1)的方法,Timer,會(huì)開啟一個(gè)死循環(huán)的線程,這樣在資源上面都有點(diǎn)浪費(fèi)。

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論