I'm writing a program similar to the producer-consumer problem. Here's my main code:
public class PipeProcessor {
private volatile boolean close = false;
Pipe pipe;
Output out;
public PipeProcessor(Pipe pipe)
{
this.pipe = pipe;
}
public void run()
{
while(!close)
{
out.output(pipe.get());
}
while(pipe.size() > 0)
out.output(pipe.get());
out.close();
}
public void close()
{
close = true;
}
}
Pipe is a wrapper for an ArrayBlockingQueue and acts as a buffer. Output is a class which takes an element in the buffer and outputs it.
I want to make sure that the PipeProcessor terminates cleanly, i.e. when it is signaled to close, it cleans the buffer. Since the close() method is called by a shutdown hook, I'm making sure that the buffer is not being filled while the processor is closing, Is this the right way to do it? Thank you.