Java – simple asynchronous I / O: many threads, one file

I have a scientific application that I usually run in parallel with xargs, but this scheme will lead to repeated JVM startup costs and ignore cached file I / O and JIT compiler I've adjusted my code to use thread pools, but I still insist on how to save the output

The program (that is, a thread of the new program) reads two files, performs some processing, and then prints the results to standard output Currently, I process the output by having each thread add its result string to the BlockingQueue As long as the boolean flag is true, another thread takes it out of the queue and writes it to the file Then I await termination and set the flag to false to trigger the file to close and exit the program

My solution seems a little clumsy; What is the simplest and best way to achieve this goal? How do I write the main result data from multiple threads to a single file?

If the answer is a widely applicable method, the answer does not need to be Java specific

to update

I'm using stop as a poison pill

while (true) {
    String line = queue.take();
    if (line.equals("STOP")) {
        break;
    } else {
        output.write(line);
    }
}
output.close();

I manually start the queue consuming thread, then add the job to the thread pool, wait for the job to complete, and finally poison the queue and join the user thread

Solution

This is how you want threads to put their output on the queue and let the writer exhaust it

What you might want to do is make things cleaner, not check a flag. Just put a "all done" flag on the queue, and the author can use it to know that it has been completed In this way, there is no necessary out of band signaling

This is simple. You can use well - known strings, enumerations, or simple shared objects

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>