android休眠后恢复线程,关于Android系统休眠后,线程的执行情况
理論上,android系統(tǒng)休眠后,app進(jìn)程會(huì)被掛起,所以相關(guān)的執(zhí)行線程也會(huì)被掛起,那些java線程的操作例如:wait,await,sleep,循環(huán)阻塞,handler的delay,線程池的delay操作都會(huì)被掛起,因?yàn)樗鼈兪褂玫南到y(tǒng)計(jì)時(shí)器在休眠的時(shí)候是停止的,例如:SystemClock.uptimeMillis(),其實(shí)針對(duì)不同版本的android系統(tǒng)這些表現(xiàn)各有不同,有些android系統(tǒng)是休眠后這些計(jì)時(shí)變慢了,原先計(jì)時(shí)5秒的,休眠后可能要計(jì)時(shí)5到6分鐘,在android系統(tǒng)中AlarmManager可以解決上述問題,鬧鐘在系統(tǒng)休眠的時(shí)候也會(huì)喚醒系統(tǒng)的,鬧鐘使用的計(jì)時(shí)器在休眠的時(shí)候是繼續(xù)跑的,例如:SystemClock.elapsedRealtime(),但是鬧鐘的計(jì)時(shí)并不是很準(zhǔn)確,甚至有秒級(jí)別的誤差:
1. setRepeating方法是重復(fù)的喚醒操作,根據(jù)api文檔可知系統(tǒng)會(huì)對(duì)這些做優(yōu)化,喚醒時(shí)間并不會(huì)嚴(yán)格的按照你設(shè)置的參數(shù)時(shí)間來(lái)執(zhí)行
2. set方法在sdk版本低于19的實(shí)現(xiàn)是嚴(yán)格準(zhǔn)確的按照設(shè)置的時(shí)間喚醒的,但是在sdk版本高于或者等于19的實(shí)現(xiàn)是經(jīng)過優(yōu)化的,并不會(huì)準(zhǔn)確按照設(shè)置的時(shí)間喚醒,所謂的優(yōu)化就是系統(tǒng)有可能判斷到間隔時(shí)間很小的有兩個(gè)鬧鐘喚醒操作,這時(shí)候系統(tǒng)可能就會(huì)自動(dòng)的把比較早的那個(gè)鬧鐘喚醒操作和比較晚的喚醒操作合并為一個(gè)
3. 根據(jù)api文檔說(shuō)明,想要精確按照設(shè)置時(shí)間喚醒可采用setExact方法,但是據(jù)我真機(jī)(小米4C,6.0系統(tǒng))實(shí)測(cè),這個(gè)方法的喚醒任然存在較大誤差,誤差甚至到秒級(jí)別,有的會(huì)誤差幾秒鐘,只能說(shuō)基本準(zhǔn)確
下面是測(cè)試代碼:
補(bǔ)充一點(diǎn):要把a(bǔ)larm和其他的定時(shí)測(cè)試分開執(zhí)行,alarm喚醒之后會(huì)影響其他線程的測(cè)試的
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休眠測(cè)試", format + ":ScheduledExecutorService執(zhí)行");
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休眠測(cè)試", format + ":Thread執(zhí)行");
}
}
}.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休眠測(cè)試", format + ":wait執(zhí)行");
}
}
}.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休眠測(cè)試", format + ":handler執(zhí)行");
}
}
};
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休眠測(cè)試", format + ":sleep執(zhí)行");
}
}
}.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休眠測(cè)試", format + ":Service執(zhí)行");
}
}, 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休眠測(cè)試", format + ":alarm執(zhí)行");
}
}
}
上面除了alarm會(huì)按時(shí)喚醒執(zhí)行,其他的在手機(jī)休眠后計(jì)時(shí)變慢了,休眠越久,誤差時(shí)間就拉的越大,都是20秒的定時(shí)任務(wù),休眠后,除了alarm,其他的誤差慢慢變大,例如一開始是30秒,然后1分鐘,3分鐘,5分鐘,10分鐘,16分鐘等等,但是這些計(jì)時(shí)延遲的時(shí)間都基本一直,說(shuō)明除了alarm,其他使用的系統(tǒng)計(jì)時(shí)器都是一致的
總結(jié)
以上是生活随笔為你收集整理的android休眠后恢复线程,关于Android系统休眠后,线程的执行情况的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 若依框架学习笔记
- 下一篇: android sina oauth2.