aspose file tools*
The moose likes Java in General and the fly likes ArrayBlockingQueue - can't put(null) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayBlockingQueue - can Watch "ArrayBlockingQueue - can New topic
Author

ArrayBlockingQueue - can't put(null)

Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

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???

Thanks in advance,
Carey
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

Thanks for the reply Steve, you helped me solidify the concept in my mind.
Carey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayBlockingQueue - can't put(null)