Java NiO getting started)

Source: Internet
Author: User
Tags ibm developerworks

Java NiO (new IO) is also a new feature introduced in JDK 1.4. Another important feature of JDK 1.4 is that regular expressions are often used, but NiO may be used, probably because the old Io system is still enough. For example, jdk1.5 has long been generic and annotated, but it is impossible to practice generics and annotations for various reasons. Nio is just keeping us away most of the time.

Compared with old Io, NiO has exclusive characteristics in some fields, so now let's have a better understanding of NiO.

NIO related APIs, in Java. NiO, it will be entangled with you in a varietyChannelAndBuffer. Related Applications are processing channels and buffers, asynchronous (non-blocking) Io, file locking, and character set processing.

Channel can be obtained by calling the getchannel () method of fileinputstream, fileoutputstream, randomaccessfile, datagramsocket, serversocket, and socket. The channel is a bit like the old I/O stream. The read and write operations of the old I/O can be directly performed on the stream, while NiO reads and writes data on various buffers.

Because the unique channel and buffer are used, NiO uses block I/O instead of the old Io stream to process the byte stream (Stream Io, therefore, block I/O is more efficient than stream I/O in bulk data processing. NIO is equivalent to localCodeTo process the I/O operation.

Asynchronous I/O makes it unnecessary for you to block threads like read ()/write () in the original way during network transmission to wait until data is written. NIO registers the listener to listen for events on the channel, and can smoothly process socket communication on a thread. Unlike the old I/O, it is not necessary to turn to polling and create many threads to process a large number of connections.

For example, NiO-based components include network application frameworks.Apache Mina, Memcached client xmemcached, Socket Application ComponentXsocketAnd Tomcat 6 also supports NiO connector.

File lock makes JavaProgramThe whole file or part of the file can also be exclusive (exclusive) or shared read or write, to prevent different processes from interfering with the same file. However, the behavior in different operating systems may be slightly different. The file lock can be obtained through the lock () or trylock () of the Channel. When the channel release () method or the channel is closed, the lock is released.

In addition, NiO can better use the control character set.

Another important buffer to NiO is the memory ing file. You can obtain mappedbytebuffer by calling the map () method of the filechannel class. This memory ing file is similar to the memory ing in windows, however, the memory ing files in windows can be used to share data between different processes. This is a Java case.

The above is a general language. Beginners may think that yunyun is not very easy to understand. It is originally a rough understanding of Java NiO, Which is messy and unordered.

There is a Java NiO getting started tutorial on IBM developerworks,Https://www6.software.ibm.com/developerworks/cn/education/java/j-nio/tutorial/index.htmlTo facilitate learning and sharing, a CHM file is created:NIO 2.16.chm.rar.

References: 1.Event-based NiO multi-thread Server
2.Servlet API and NiO: Combined
3.Principles and usage of Java NiO
4.Use Java NiO to write high-performance servers
5.Java NiO API details
6.NIO file read/write

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.