BlockingQueue on main Service thread

My android app has a long running background service, which I also understand runs in the application’s main thread and for that reason, any time consuming or blocking task should be moved to a separate thread.

Now, here is the situation, I don’t understand/confused about:

When I bind to the service from an activity, i receive an reference to the service which allows me to invoke service methods from my activity. One of the methods allows me to pass a String object from the activity to the service, which is then added to a BlockingQueue. A separate worker thread which is started in the Service’s onCreate method, checks the queue for available data and then performs the required task.

What I want to understand is, if at some point, the queue becomes full and an attempt to the queue blocks, will it affect the main thread the service is running on?


Yes. In this situation, if the queue becomes full, the calling thread will block (in your situation, the main thread). So this is a bad design.

The produced data coming from a field of an Activity doesn’t force you to use it on the main thread. I suggest you use some Handler for your producer running on its own thread which will allow you to make the processing (and eventually waiting on the queue) outside of the main thread.

This is also good for communicating with your Service since you can use Handlers to communicate with a Service (see Android Services’ guide).

Finally, if applying the produced data can be passed directly to an Handler using either
or Handler.send(Message)

Leave a Reply

Your email address will not be published. Required fields are marked *