Reputation: 4447
I have an event processing scheme which should also eventually write to file; I cannot afford to delay the event when the file is being flushed, i.e. waiting to the end of BufferedWriter.write(String).
I'm looking for the easiest way to achieve that (is there's a library doing that? I reckon that I'm not the only one who had this problem)
Upvotes: 3
Views: 1356
Reputation: 147164
So long as you are keeping the same thread, you can use java.io.PipedOutputStream
to store the data and have separate thread copy from the matching PipedInputStream
to file.
Upvotes: 1
Reputation: 29139
You could use an single threaded executor to perform the file writing for each event.
ExecutorService executor = Executors.newSingleThreadExecutor();
// for each event
executor.submit(new Runnable() {
public void run()
{
// write to the file here
}
});
There would only be one thread and the executor would take care of queueing.
Upvotes: 5
Reputation: 2715
You could make a queue based system, where you put the events on a queue/list and then have another thread which takes the events to be written, and write them out. This way the file writer will be asynchronous to the rest of the system, and your only delay will be adding an element to a list.
Upvotes: 0
Reputation: 165292
Basically, you want the writing to the file not interrupt your flow of event handling.
In this case, all you need to do is delegate the file handling away to a separate thread.
Your code should look something like this:
// event handling starts
Runnable fileHandlingThread = new Runnable() {
public void run() {
// open the file
// write to the file
// flush the file
}
};
new Thread(fileHandlingThread).start();
// continue doing other things in the mean time
Upvotes: 2