[android-developers] sendBroadcast in IntentServicenot non-re-entrant?

2010-11-04 Thread Bret Foreman
I'm trying an experiment where an Activity makes a number of
consecutive calls to an IntentService. Each call kicks off some
background work that finishes in a few seconds. So there are multiple
concurrent instances of the IntentService running. All the instances
finish at around the same time and each one does a sendBroadcast to
return a message to the Activity that it's finished. When this
happens, about 30% of the broadcast intents never get back to the
Activity's registered receiver.

It looks as if there is some static state inside the IntentService
class that is in conflict when there are multiple invocations running
and they are all calling sendBroadcast at about the same time. In
other words, sendBroadcast appears to be non-re-entrant.

I'd like to package up this example for submission to b.android.com
but before I do, I'd like to find out a few things from this forum:

1) Is this a known bug?
2) Is this expected behavior (ie not a bug)?
3) Has anyone seen anything like this or another case where the
IntentService worked correctly when used in this way?

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] sendBroadcast in IntentServicenot non-re-entrant?

2010-11-04 Thread Kostya Vasilyev

Bret,

You saying that there are multiple concurrent instances of the 
IntentService running is not entirely accurate.


If you actually defined multiple services in Java / the manifest, my 
apologies.


If you only defined one service, then there is only one instance of the 
service, and any intents it receives are queued up to a thread-safe 
queue as soon as they are received. The worker thread (there is only 
one, AFAIK) then de-queues intents from this queue and processes them 
one by one.


So no synchronization issues there.

However, it's not out of the question that calling sendBroadcast, with 
it being a framework function, is only allowed from the UI thread, just 
like many other framework functions in Android.


Since you are able to consistently reproduce this problem in your 
application, I think it wouldn't hurt to run a simple experiment.


Change calls to sendBroadcast so they are made from the UI thread (e.g. 
obtain a Handler in your service's onCreate, then post a runnable 
containing the intent to be broadcast to the Handler).


Also: for the case you described, you could use a lighter-weight 
notification mechanism, such as ResultReceiver or Message.


-- Kostya

04.11.2010 20:54, Bret Foreman пишет:

I'm trying an experiment where an Activity makes a number of
consecutive calls to an IntentService. Each call kicks off some
background work that finishes in a few seconds. So there are multiple
concurrent instances of the IntentService running. All the instances
finish at around the same time and each one does a sendBroadcast to
return a message to the Activity that it's finished. When this
happens, about 30% of the broadcast intents never get back to the
Activity's registered receiver.

It looks as if there is some static state inside the IntentService
class that is in conflict when there are multiple invocations running
and they are all calling sendBroadcast at about the same time. In
other words, sendBroadcast appears to be non-re-entrant.

I'd like to package up this example for submission to b.android.com
but before I do, I'd like to find out a few things from this forum:

1) Is this a known bug?
2) Is this expected behavior (ie not a bug)?
3) Has anyone seen anything like this or another case where the
IntentService worked correctly when used in this way?




--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] sendBroadcast in IntentServicenot non-re-entrant?

2010-11-04 Thread Dianne Hackborn
On Thu, Nov 4, 2010 at 1:00 PM, Kostya Vasilyev kmans...@gmail.com wrote:

 However, it's not out of the question that calling sendBroadcast, with it
 being a framework function, is only allowed from the UI thread, just like
 many other framework functions in Android.


sendBroadcast can be called from any thread.

There are no issues I know of with sendBroadcast dropping broadcasts,
outside of the expected behavior I mentioned earlier.

IntentService itself is extremely simple, and just enqueues each
onStartCommand() to a handler.  It is very unlikely things are being lost at
that point.

An interesting test case showing a problem would be something that derives
directly from Service, showing that a call to startService() does not result
in the given Intent appearing in Service.onStartCommand().

-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en