【发布时间】:2012-03-28 00:09:10
【问题描述】:
我刚刚在 Android 官方网站上找到了以下代码:
@Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
}
我还阅读了以下论文:
- 创建一个默认工作线程,该线程执行传递给 onStartCommand() 的所有意图,与应用程序的主线程分开。
- 创建一个工作队列,一次将一个意图传递给您的 onHandleIntent() 实现,因此您不必担心多线程。
所以如果 IntentService 使用工作线程并且我永远不必担心多线程,那么为什么我需要在 onHandleIntent(...) 方法中使用同步块?谢谢。
【问题讨论】: