android – IntentService的onStartCommand(..)方法线程安全吗?

我有一个 IntentService类,可以从复杂应用程序中的各个位置启动 – 活动,后台线程,其他服务.我想知道调用服务的次数.我在我的IntentService类中使用私有int变量来跟踪它,我从我的 onStartCommand(…)方法增加它.

它只是发生在我身上,因为可以从各种异步线程调用onStartCommand(…)方法,这可能不是一个线程安全的解决方案.所以问题是,我是否需要在同步块中包含对此计数器变量的访问,或者onStartCommand(…)的IntentService实现是否为我处理这个问题?

作为一个注释,我知道我可以安全地从onHandleIntent(…)增加变量,但我需要计算实际请求而不是执行的意图.

解决方法

It just occured to me,as the onStartCommand(…) method can be called from various asynchronous threads,that this might not be a thread-safe solution.

onStartCommand()总是在任何服务的主应用程序线程上调用.不能同时在两个线程中使用onStartCommand()调用.

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...