1
我刚刚发现下面就Android官方网站代码:扩展IntentService类
@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(...)方法中使用同步块?谢谢。
非常感谢。 – user1166635 2012-03-27 16:18:11