android adm查看进程,基于android studio 的ADM对卡顿,耗时方法的检测

Message next() {

// 当消息循环已经退出或被释放的时候,直接返回null

// 如果应用程序在退出后尝试重新启动一个looper,就可能会发生这种情况

final long ptr = mPtr;

if (ptr == 0) {

return null;

}

int pendingIdleHandlerCount = -1; // -1 only during first iteration

int nextPollTimeoutMillis = 0;

for (;;) {//死循环

//将当前线程中的任何阻塞的binder命令刷新到cpu中执行,在执行可能导致长时间阻塞的操作前调用是非常有用的,可以确保任何挂起的对象引用被释放,防止进程持有一个对象的时间超过它所需要的

if (nextPollTimeoutMillis != 0) {

Binder.flushPendingCommands();

}

//消息死循环不会崩溃的原因

nativePollOnce(ptr, nextPollTimeoutMillis);

//同步代码块

synchronized (this) {

// 尝试检索下一条信息,如果有就返回

final long now = SystemClock.uptimeMillis();

Message prevMsg = null;

Message msg = mMessages;

if (msg != null && msg.target == null) {

// Stalled by a barrier. 在消息队列中查找下一个异步消息

do {

prevMsg = msg;

msg = msg.next;

} while (msg != null && !msg.isAsynchronous());

}

if (msg != null) {

if (now < msg.when) {

//如果下一条消息尚未准备好。设置超时唤醒

nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);

} else {

// 如果准备好了就获取消息.

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 {

// 没有异步消息了

nextPollTimeoutMillis = -1;

}

// Process the quit message now that all pending messages have been handled.

if (mQuitting) {

dispose();

return null;

}

// 如果第一次空闲,则获取可运行消息数

// 空闲消息只会在消息队列为空或者队列第一个消息将要被处理时处理

if (pendingIdleHandlerCount < 0

&& (mMessages == null || now < mMessages.when)) {

pendingIdleHandlerCount = mIdleHandlers.size();

}

if (pendingIdleHandlerCount <= 0) {

// No idle handlers to run. Loop and wait some more.

mBlocked = true;

continue;

}

if (mPendingIdleHandlers == null) {

//容量只有四个

mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];

}

mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);

}

// Run the idle handlers.

//只有在第一次迭代中才到达这个代码块

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) {

synchronized (this) {

//当返回值keep为false时,从空闲列表中移除

mIdleHandlers.remove(idler);

}

}

}

// 保证了空闲列表中的消息只处理一次.

pendingIdleHandlerCount = 0;

// 在调用空闲处理程序时,可能已经传递了一个新消息,因此可以返回并再次查找挂起的消息,而不必等待

nextPollTimeoutMillis = 0;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值