4

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?

8
  • 1
    see original SO question stackoverflow.com/questions/10408055/…
    – Eran Medan
    May 2, 2012 at 6:53
  • 2
    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, 2012 at 7:09
  • 1
    @BlackJack is correct, you need to figure out where your performance bottleneck is before trying to optimize it. May 2, 2012 at 7:18
  • 2
    @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. May 2, 2012 at 12:23
  • 2
    Are you using Java 7? It has Asynchronous I/O built into the new File System APIs May 2, 2012 at 13:52

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.