【发布时间】:2013-09-01 22:05:08
【问题描述】:
我最近重新构建了我的应用的电源管理部分以降低复杂性。其中的变化是唤醒锁的重用;具体来说,线程在创建时接收一个唤醒锁,然后根据需要获取/释放它,直到被杀死。我发现这会导致唤醒锁在调用release() 时并不总是释放。导致问题的代码本质上是在这里给出的:
// Get the lock for the first time, acquire it, and do some work.
WakeLock wakelock = receiveFirstWakeLock();
wakelock.acquire();
doWork();
// When work is finished, release the lock.
// Typically this lock is released very quickly.
wakelock.release();
// Re-acquiring the lock for the next bout of work always works.
wakelock.acquire();
doWork();
// In my full code, "wakelock" didn't appear to be releasing properly.
// I hypothesized that it might just be taking a little while
// so I tried this to see how long it would take.
// I found that it sometimes *never* releases!
wakelock.release();
while (wakelock.isHeld())
Thread.yield();
Log.d("Test","Released the lock!");
我的问题是:
- 还有其他人遇到过这种情况吗?
- 唤醒锁是否意味着以这种方式重用?
- 如果 #2 是,这是一个错误吗?
- 如果 #2 没有,我是否在文档中的某个地方错过了这一点?
【问题讨论】:
-
您确定第二个 doWork() 不会阻塞并因此阻止执行发布吗?你的 Wakelock 使用什么标志?
-
它绝对不会阻塞——即使在比这复杂得多的完整代码中(这是一个工作循环)。总是到达第二个 wakelock.release()。它有时只是不释放,但似乎总是在第一次到达时释放。我正在使用 PARTIAL_WAKE_LOCK。
标签: java android wakelock android-wake-lock