关于Android系统休眠后,线程的执行情况

理论上,android系统休眠后,app进程会被挂起,所以相关的执行线程也会被挂起,那些java线程的操作例如:wait,await,sleep,循环阻塞,handler的delay,线程池的delay操作都会被挂起,因为它们使用的系统计时器在休眠的时候是停止的,例如:SystemClock.uptimeMillis(),其实针对不同版本的android系统这些表现各有不同,有些android系统是休眠后这些计时变慢了,原先计时5秒的,休眠后可能要计时5到6分钟,在android系统中AlarmManager可以解决上述问题,闹钟在系统休眠的时候也会唤醒系统的,闹钟使用的计时器在休眠的时候是继续跑的,例如:SystemClock.elapsedRealtime(),但是闹钟的计时并不是很准确,甚至有秒级别的误差:
1. setRepeating方法是重复的唤醒操作,根据api文档可知系统会对这些做优化,唤醒时间并不会严格的按照你设置的参数时间来执行
2. set方法在sdk版本低于19的实现是严格准确的按照设置的时间唤醒的,但是在sdk版本高于或者等于19的实现是经过优化的,并不会准确按照设置的时间唤醒,所谓的优化就是系统有可能判断到间隔时间很小的有两个闹钟唤醒操作,这时候系统可能就会自动的把比较早的那个闹钟唤醒操作和比较晚的唤醒操作合并为一个
3. 根据api文档说明,想要精确按照设置时间唤醒可采用setExact方法,但是据我真机(小米4C,6.0系统)实测,这个方法的唤醒任然存在较大误差,误差甚至到秒级别,有的会误差几秒钟,只能说基本准确
下面是测试代码:
补充一点:要把alarm和其他的定时测试分开执行,alarm唤醒之后会影响其他线程的测试的

public class MainActivity extends AppCompatActivity {

    ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "sss", Toast.LENGTH_SHORT).show();
            }
        });
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
                NotificationUtil.showNotification(MainActivity.this, new Intent(), 100, "ScheduledExecutorService休眠测试", format + ":ScheduledExecutorService执行");
                i++;
            }
        }, 20, 20, TimeUnit.SECONDS);
        new Thread() {
            @Override
            public void run() {
                long now;
                while (true) {
                    now = SystemClock.elapsedRealtime();
                    while (SystemClock.elapsedRealtime() - now < 20000) ;
                    String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
                    NotificationUtil.showNotification(MainActivity.this, new Intent(), 101, "Thread休眠测试", format + ":Thread执行");
                }
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (this) {
                        try {
                            this.wait(20000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
                    NotificationUtil.showNotification(MainActivity.this, new Intent(), 102, "wait休眠测试", format + ":wait执行");
                }
            }
        }.start();

        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                sendEmptyMessageDelayed(1, 20000);
                if (msg.what == 1) {
                    String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
                    NotificationUtil.showNotification(MainActivity.this, new Intent(), 103, "handler休眠测试", format + ":handler执行");
                }
            }
        };
        handler.sendEmptyMessageDelayed(1, 20000);

        new Thread(){
            @Override
            public void run() {
                while (true) {
                    try {
                        sleep(20000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
                    NotificationUtil.showNotification(MainActivity.this, new Intent(), 104, "sleep休眠测试", format + ":sleep执行");
                }
            }
        }.start();

        startService(new Intent(this, TestService.class));
    }
}


public class TestService extends Service {

    ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        HandlerThread handlerThread = new HandlerThread("ht");
        handlerThread.start();
        Handler handler = new Handler(handlerThread.getLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    scheduledExecutorService.schedule(new Runnable() {
                        @Override
                        public void run() {
                            String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
                            NotificationUtil.showNotification(TestService.this, new Intent(), 105, "Service休眠测试", format + ":Service执行");
                        }
                    }, 0, TimeUnit.SECONDS);
                }
            }
        });
        Intent intent = new Intent(this, InnerGuardReceiver.class);
        intent.setAction("com.xtc.watch.guard.push");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, -1001, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 20000, 20000, pendingIntent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    public static class InnerGuardReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
            NotificationUtil.showNotification(context, new Intent(), 106, "alarm休眠测试", format + ":alarm执行");
        }
    }
}

上面除了alarm会按时唤醒执行,其他的在手机休眠后计时变慢了,休眠越久,误差时间就拉的越大,都是20秒的定时任务,休眠后,除了alarm,其他的误差慢慢变大,例如一开始是30秒,然后1分钟,3分钟,5分钟,10分钟,16分钟等等,但是这些计时延迟的时间都基本一直,说明除了alarm,其他使用的系统计时器都是一致的

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值