Java – How to Read from a Database and Write to a File Asynchronously

concurrencyiojavamultithreading

I'm trying to modify a serial program that reads from a database, and writes results to a file, this is done in a blocking way and I think we can get performance boost of there is a memory buffer and have the file being written in the "background" asynchronously

I can think of the "job interview" solution, using Threads, shared resource, syncronized blocks etc… but I'm sure there is a better way (is there a nice little "delayed write" library out there that will do it for me?)

Does any of the java.util.concurrent package offer any help? java.nio? or perhaps JMS/ActiveMQ?

What about PipedOutputStream / PipedInputStream as a basis for my buffer?

How do I implement a delayed / background / buffered / non-blocking / asynchronous file writter in Java?

Best Answer

It's been a while, but perhaps this article on JSR 203 may offer some guidance. An example of what you may be after is given as:

AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("Path to file"));
ByteBuffer buffer = ByteBuffer.allocate(capacity);

Future result = channel.write(buffer, 100); // Write capacity bytes to the file starting at position 100

boolean done = result.isDone(); //Indicate if the result is already terminated
// Alternatively...
int bytesRead = result.get(); // You can also wait for completion

Of course, before going down this route you should consider that you are not indulging in premature optimisation. Make sure you have proof that an asynchronous operation will actually benefit your application.

Related Topic