Java NIO Overview

 

Jakob Jenkov
Last update: 2014-06-23

Java NIO consist of the following core components:

  • Channels
  • Buffers
  • Selectors

Java NIO has more classes and components than these, but the ChannelBuffer and Selector forms the core of the API, in my opinion. The rest of the components, like Pipe and FileLock are merely utility classes to be used in conjunction with the three core components. Therefore, I'll focus on these three components in this NIO overview. The other components are explained in their own texts elsewhere in this tutorial. See the menu at the top corner of this page.

ChannelBuffer and Selector  是三个主要的东西,Pipe and FileLock 是要和这三个一起使用的

Channels and Buffers

如图所示,Channel有点像流,从buffer里读写数据

Typically, all IO in NIO starts with a Channel. A Channel is a bit like a stream. From the Channel data can be read into a Buffer. Data can also be written from a Buffer into a Channel. Here is an illustration of that:

Java NIO: Channels read data into Buffers, and Buffers write data into Channels

There are several Channel and Buffer types. Here is a list of the primary Channel implementations in Java NIO:

Channel有如下几中类型,覆盖了 TCP,UDP,文件io

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

As you can see, these channels cover UDP + TCP network IO, and file IO.

There are a few interesting interfaces accompanying these classes too, but I'll keep them out of this Java NIO overview for simplicity's sake. They'll be explained where relevant, in other texts of this Java NIO tutorial.

Here is a list of the core Buffer implementations in Java NIO:

一些buffer类型

  • ByteBuffer
  • CharBuffer
  • DoubleBuffer
  • FloatBuffer
  • IntBuffer
  • LongBuffer
  • ShortBuffer

These Buffer's cover the basic data types that you can send via IO: byte, short, int, long, float, double and characters.

Java NIO also has a MappedByteBuffer which is used in conjunction with memory mapped files. I'll leave this Buffer out of this overview though.

Selectors

一个selector可以管理多个Channel,这个非常适合,你的connection很多,但是么个连接的流量又很低的情况。如聊天

Selector allows a single thread to handle multiple Channel's. This is handy if your application has many connections (Channels) open, but only has low traffic on each connection. For instance, in a chat server.

Here is an illustration of a thread using a Selector to handle 3 Channel's:

Java NIO: A Thread uses a Selector to handle 3 Channel's

To use a Selector you register the Channel's with it. Then you call it's select() method. This method will block until there is an event ready for one of the registered channels. Once the method returns, the thread can then process these events. Examples of events are incoming connection, data received etc.

要用selector你需要往它里面注册Channel,通过select()方法,这个方法会一直阻塞到待注册通道准备好,方法返回,这个线程就可以做其他的事情了。

Java NIO Overview的更多相关文章

  1. Java NIO 完全学习笔记(转)

    本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ...

  2. Java NIO Tutorial

    Java NIO Tutorial     Jakob JenkovLast update: 2014-06-25

  3. Java NIO:NIO概述

    Java NIO:NIO概述 在上一篇博文中讲述了几种IO模型,现在我们开始进入Java NIO编程主题.NIO是Java 4里面提供的新的API,目的是用来解决传统IO的问题.本文下面分别从Java ...

  4. [翻译]java nio 概述

    原文地址:http://tutorials.jenkov.com/java-nio/overview.html java NIO 包含一下核心内容: Channels Buffers Selector ...

  5. Java NIO学习笔记一 Java NIO概述

    Java NIO概述 Java NIO(新的IO)是Java的替代IO API(来自Java 1.4),这意味着替代标准的 java IO和java Networking API.Java NIO提供 ...

  6. Java NIO核心组件简介

    原文链接:http://tutorials.jenkov.com/java-nio/overview.html NIO包含下面几个核心的组件: Channels Buffer Selector 整个N ...

  7. Java NIO学习笔记---I/O与NIO概述

    文章目录: 1.什么是IO 2.什么是Java NIO 3.I/O常见概念 4.为什么使用NIO 5.IO VS NIO 一.什么是IO I/O 或者输入/输出 , 指的是计算机与外部世界或者一个程序 ...

  8. Java NIO (1)

    Java NIO (1) 看了下java核心技术这本书 关于nio的部分介绍比较少,而且如果自己写服务器的话nio用的还是比较多,整理一下nio的资料 java中nio主要是三个组件 Buffers ...

  9. 【转载】Java NIO学习 & NIO BIO AIO 比较

    可以参考这个页面: http://www.iteye.com/magazines/132-Java-NIO (下面这个页面也有) http://ifeve.com/overview/ 另,在这篇文章里 ...

随机推荐

  1. PYTHON语言书库

    <SELENIUM 2 自动化测试实战 基于PYTHON语言>pdf 附下载链接 本书下载链接:https://pan.baidu.com/s/1A1s6UHecXd1Z465zbLumh ...

  2. [Lua]table(一):打印与复制

    一.打印table function PrintTable(tb) if type(tb) ~= "table" then print(tb) return end local c ...

  3. 关系型数据库和NoSQL数据库

    一.数据库排名和流行趋势 1.1 Complete ranking 链接: https://db-engines.com/en/ranking 在这个网站列出了所有数据库的排名,还可以看到所属数据库类 ...

  4. 重识linux-linux系统服务相关

    重识linux-linux系统服务相关 1 tcp wrappers 特殊功能  应用级防火墙 2 系统开启的服务查看 top,ps 命令 3 查看系统启动的服务 1) 找到目前系统开启的网络服务 n ...

  5. 如何删除GitHub中已经建好的仓库(repository)

    我们有时候可能需要清理 GitHub 中一些不维护的或不需要的项目,此时就要用到delete操作了,很多新手可能不知道如何删除已有仓库,下面将简单介绍下,需要注意的是删除操作不能恢复,一旦执行此操作, ...

  6. layui动态options

    layui 坑 layui 针对各种表单元素做了较为全面的UI支持,你无需去书写那些 UI 结构,你只需要写 HTML 原始的 input.select.textarea 这些基本的标签即可.我们在 ...

  7. Tomcat相关的配置和设置

    1.Tomcat环境配置方法见百度文库.略. 2.查看. webapps:所有课执行的WEB项目都放在次目录中 work:此文件保存了所有临时文件,当开发中发现一个程序无法正确执行的时候,就可以考虑将 ...

  8. django之relacted.py(探秘django的关联field)

    生成model类对象时,传入的每个field对象都会调用其contribute_to_class函数,生成对应的属性. def contribute_to_class(self, cls, name, ...

  9. ubuntu 上 SSH scp 技巧

    参考:https://deepzz.com/post/how-to-setup-ssh-config.html SSH(Secure Shell)是什么?是一项创建在应用层和传输层基础上的安全协议,为 ...

  10. Redis位图实现用户签到功能

    场景需求 适用场景如签到送积分.签到领取奖励等,大致需求如下: 签到1天送1积分,连续签到2天送2积分,3天送3积分,3天以上均送3积分等. 如果连续签到中断,则重置计数,每月初重置计数. 当月签到满 ...