I'm using a ArrayBlockingQueue to queue up input from a pipe. When I reach the end of the pipe input (as evidenced by the returning of a null) I want to mark the end of the queue with something that the consumer can take() and determine that there will be no more data to follow. I wanted to put a null on the queue but then I get a NullPointerException in the java.io library. As a work-around I'm adding an empty string ("") which the consumer then detects. Currently an empty string doesn't conflict with any real data so this works, but I don't feel that this is a proper long term solution. Any other ideas???
It is typical to put a 'TerminalObject' into the Queue when the end of Production has occurred. It is even described in BlockingQueue API. The best value for the termination signal depends on what you are putting through the Queue. If your Queue is a BlockingQueue<String> then using an empty String as a termination value is as good as any.
Another option would be to have a separate thread that waits for the producer to end, and when it does, interrupts the consumer (un-blocking it) and signals for it to stop.