4. 특히나 IntentService는?
IntentService는 Intent를 전달 받을 때 마다, 해당 Intent를 처리하도록
특화되어 있는 Service 중 하나입니다.
예를 들어서, 특정 데이터를 전송하고, 해당 데이터를 저장을 하는 작업과
같은 작업을 IntentService를 통해서 구현할 수 있습니다.
IntentService에 대한 자세한 내용:
http://developer.android.com/reference/android/app/IntentService.html
이 Service 경우 자체가 가지는 특수성에 따라서,
Intent에 대해서 동시 다발적으로 처리가 수행이 되는지가 중요합니다.
이에 대해서 알아보도록 합니다.
5. IntentService Overview
IntentService is a base class for Services that handle asynchronous
requests (expressed as Intents) on demand. Clients send requests
throughstartService(Intent) calls; the service is started as needed,
handles each Intent in turn using a worker thread, and stops itself
when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from
an application's main thread. The IntentService class exists to simplify this
pattern and take care of the mechanics. To use it, extend IntentService and
implement onHandleIntent(Intent). IntentService will receive the Intents,
launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as
long as necessary (and will not block the application's main loop), but only
one request will be processed at a time.
6. 결론은!
IntentService는 Intent를 가지고 여러 번 호출이 되더라도,
한 번에 하나씩 요청이 인입된 순서에 따라서 처리를 진행합니다.
처리할 Intent가 없으면, Service는 사라지고
신규 Intent가 도착하여 처리할 사항이 있으면 Service가 1개 신규 생성됩니다.
만약 Intent가 여러 개가 쭈욱 밀려 있으며,
처음 생성된 Intent가 모든 Intent를 순차적으로 처리를 하게 됩니다.
즉, IntentService는 하나의 Instance가 순차적으로 처리를 진행합니다.
7. 테스트 진행
Service start가 호출될 때마다 Instance가 생성되어
호출이 되게 되면,
Test time이 호출되는 내역이 번갈아 나타날 것이고
Instance 하나가 나타나면 순차적으로 나타날 것입니다.
8. 테스트 결과
수행 1. 3회 연달아 서비스 호출
수행 2. 3회 처리 후 서비스 2회 연달아 호출
Thread ID.
처리할 작업이 없어지니 사라지고
새 처리 작업이 발생되니 새로 생성 확인
요청이 Multi가 아닌 Single로
순차적으로 처리됨을 확인
9. 덧붙이자면~
Service에 대해서 동일한 테스트를 한 결과.
Instance는 하나만 생성이 되고, Service가 제공이 됨이 확인되었습니다.
그렇지만, Service는 InstanceService와 별도로 Thread 하나가
메모리 상태에 꾸준히 떠 있는 데몬 형태와 같이 구동을 함을 확인 했습니다.
동일하게 3회/2회 연달아서 테스트. Thread ID 동일 확인