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?
-
1see original SO question stackoverflow.com/questions/10408055/…Eran Medan– Eran Medan2012年05月02日 06:53:43 +00:00Commented May 2, 2012 at 6:53
-
2What 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– BlackJack2012年05月02日 07:09:36 +00:00Commented 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.Martin Wickman– Martin Wickman2012年05月02日 07:18:51 +00:00Commented 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.Gilbert Le Blanc– Gilbert Le Blanc2012年05月02日 12:23:51 +00:00Commented May 2, 2012 at 12:23
-
2Are you using Java 7? It has Asynchronous I/O built into the new File System APIsMartijn Verburg– Martijn Verburg2012年05月02日 13:52:34 +00:00Commented May 2, 2012 at 13:52
1 Answer 1
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.