I have a service that I would like to implement as a Google Guava Service.
The service basically runs a while (true) loop that processes events as they arrive on a BlockingQueue. Simplified sample code is available here:
https://gist.github.com/3354249
The problem is that the code blocks on BlockingQueue#take(), so the only way to stop the service is to interrupt its thread. Is this possible using Guava’s AbstractExecutionThreadService?
Of course, in this case I could replace queue.take() with a polling loop using queue.poll(1, TimeUnit.SECONDS), thus removing the need for thread interruption. However:
-
I would like to avoid doing this, for both performance and code readability reasons
-
There are other cases where it is impossible to avoid thread interruption, e.g. if the service is blocked while reading bytes from an
InputStream.
I don’t think interrupting the thread is really an option if you want to use an
AbstractExecutionThreadServicesince there’s not really any way to get a reference to the thread in order to callinterrupt().If you’re using a BlockingQueue you either have to poll inside a while loop that checks if the service is still running, or you can use a sentinel value to alert the worker method that it needs to stop.
Examples:
Polling:
Sentinal value:
I personally would try the polling solution and see what the performance is like. You might be surprised by how little that really effects the performance.
As for reading from an InputStream, if the InputStream is long-lived and has the potential to block indefinitely I don’t think using an
AbstractExecutionThreadServiceis really possible. You should instead use anAbstractServicewhich creates and holds a reference to its own execution thread so that you can interrupt it in thedoStop()method.