Java's NIO programming

Source: Internet
Author: User

The so-called programming, essays like Java files, the article is like a class, the reference is import, then the directory is the method definition.

This article is in the analysis of Thrift's nonblocking server, because the latter relies on the knowledge of the article. If two articles are the same, that is, two classes share the same file, one of which must be public, if the article in another article, that is, the inner class. According to the programming specifications, or divided into two pieces to write as well.

The Java NiO detailed series of articles, better, or recommend this article http://tutorials.jenkov.com/java-nio/overview.html. This article is just a brief summary. One for the memo, two for forgetting to be able to pick up soon. Therefore, the view of the blog is written, most of the sketch, point to stop. So what? The so-called language programming, framework tools, asynchronous requests, various protocols, model evolution, parameter configuration, with the use of with abandon, with the pick up, turn hand for the cloud, cover hand for the rain, more than three layers, save without discussion, three layers and down, Qin Qin exercises. So what? Confucius said: The gentleman does not have the device!

Above the wordy, the following began to get to the point.

The main components of Java NiO are buffer, Channel, Selector. Buffer corresponds to the data, the channel corresponds to the connection, and the selector is the manager and monitor of all the channel to which it is registered.

Compared with Java Io, the former (IO) is like live, the latter is playing on its own, can go forward, can be backward, the former operation of each connection is blocked, the general practice is to open a single thread to handle a connection, such as the previous ThreadPool server approach, The latter can manage multiple connections in one thread, that is, by managing the channel in the name of the selector event, and moving the blocking up to the selector's Select function.

Using buffer, you can manipulate the channel's read and write, use selector to monitor the channel's events, and put a piece of code below, which means self-explanatory.

Selector Selector = Selector.open (); channel.configureblocking (false); Selectionkey key = Channel.register (selector, selectionkey.op_read); while (true) {  int readychannels = Selector.select ();  if (Readychannels = = 0) continue;  set<selectionkey> Selectedkeys = Selector.selectedkeys ();  iterator<selectionkey> keyiterator = Selectedkeys.iterator ();  while (Keyiterator.hasnext ()) {    Selectionkey key = Keyiterator.next ();    if (key.isacceptable ()) {        //A connection is accepted by a serversocketchannel.    } else if (Key.isconnectable ()) { c9/>//A connection is established with a remote server.    } else if (key.isreadable ()) {        //A channel is ready for reading    } else if (Key.iswritable ()) {        //a channel is writing    }    keyiterator.remove ();}  }

The more useful tool classes associated with channel reading and writing are scatter/gather. Can read more, you can write one more.

Complete.

Java's NIO programming

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.