Take the 2-minute tour ×

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?

share|improve this question
see original SO question stackoverflow.com/questions/10408055/… –  Eran Medan May 2 '12 at 6:53
What kind of time period? There are solutions to your problem, but if you want a "performance boost," first you should tell us what kind of performance you get as a baseline. –  BlackJack May 2 '12 at 7:09
@BlackJack is correct, you need to figure out where your performance bottleneck is before trying to optimize it. –  Martin Wickman May 2 '12 at 7:18
@Eran Medan: Not really. If you make multiple changes while you're waiting for production stats, and you get a slight improvement, which of the multiple changes had the biggest impact? Experimenting with one change at a time, and measuring the results, gives better results. –  Gilbert Le Blanc May 2 '12 at 12:23
Are you using Java 7? It has Asynchronous I/O built into the new File System APIs –  Martijn Verburg May 2 '12 at 13:52
show 3 more comments

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer


