I’m looking for a simple producer – consumer implementation in Java and don’t want to reinvent the wheel
I couldn’t find an example that uses both the new concurrency package and either of the Piped classes
Is there an example for using both PipedInputStream and the new Java concurrency package for this?
Is there a better way without using the Piped classes for such a task?
For your task it might be sufficient to just use a single thread and write to the file using a
BufferedOutputStreamas you are reading from the database.If you want more control over the buffer size and the size of chunks being written to the file, you can do something like this:
And in the calling code:
Note that if this is all you’re doing, there isn’t any advantage to using an
ExecutorService. Executors are useful when you have many tasks (too many to launch all of them in threads at the same time). Here you have only two threads that have to run at the same time, so callingThread#startdirectly will have less overhead.