Java io: Concurrent IO

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.