Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayBlockingQueue - can't put(null)

 
Carey Brown
Ranch Hand
Posts: 1540
18
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Ranch Hand
Posts: 1540
18
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Steve, you helped me solidify the concept in my mind.
Carey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic