我最近重建了我的应用程序的电源管理部分以降低复杂性。其中的变化是唤醒锁的重用;具体来说,线程在创建时接收一个唤醒锁,然后根据需要获取/释放它,直到被杀死。我发现这会导致唤醒锁在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,我是否在文档中的某个地方错过了这个?