Original link Author: Jakob Jenkov translator: Li Jing
Sometimes you may need to process input and output concurrently. In other words, you may have more than one thread handling input and generating output. For example, you have a program that needs to handle a large number of files on the disk, and this task can improve performance through concurrent operations. Another example is that you have a Web server or chat server that receives many connections and requests that can be boosted by concurrency.
If you need to process IO concurrently, here are a few questions to consider:
No multiple threads can read data from InputStream or reader at the same time, nor can they write data to OutputStream or writer simultaneously. You have no way of guaranteeing how much data each thread reads, and the order in which multiple threads write data.
If the order of operations is guaranteed between threads, they can use the same stream, reader, writer. For example, you have a thread that determines what type of request the current input stream is from, and then passes the stream data to other appropriate threads for subsequent processing. This approach is feasible when accessing streams, reader, and writer in an orderly manner. Note that the code that passes the stream data between threads should be synchronous.
Note: In Java NIO, you can have a thread read and write to multiple "channel". For example, you have a lot of internet connections on, but only a small amount of data per connection, like a chat server, allows a thread to monitor multiple channels (connections). Java NiO is another topic that will be covered in a follow-up tutorial.
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java io: Concurrent IO
Java io: Concurrent IO