Java NIO Overview
2016-03-12 19:48
549 查看
Java NIO consist of the following core components:
Channels
Buffers
Selectors
Java NIO has more classes and components than these, but the
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.
Typically, all IO in NIO starts with a
Here is an illustration of that:
There are several
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
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
These
Java NIO also has a
A
Here is an illustration of a thread using a
To use a
then process these events. Examples of events are incoming connection, data received etc.
Channels
Buffers
Selectors
Java NIO has more classes and components than these, but the
Channel,
Bufferand
Selectorforms the core of the API, in my opinion. The rest of the components, like
Pipeand
FileLockare 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.
Channels and Buffers
Typically, all IO in NIO starts with a Channel. A
Channelis a bit like a stream. From the
Channeldata can be read into a
Buffer. Data can also be written from a
Bufferinto a
Channel.
Here is an illustration of that:
Java NIO: Channels read data into Buffers, and Buffers write data into Channels |
Channeland
Buffertypes. Here is a list of the primary
Channelimplementations in Java NIO:
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
Bufferimplementations in Java NIO:
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
MappedByteBufferwhich is used in conjunction with memory mapped files. I'll leave this
Bufferout of this overview though.
Selectors
A Selectorallows 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
Selectorto handle 3
Channel's:
Java NIO: A Thread uses a Selector to handle 3 Channel's |
Selectoryou 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.
相关文章推荐
- 23中java设计模式之单例模式
- Java NIO Tutorial
- 和Eclipse一起走过的日子
- eclipse启动失败
- Eclipse中将classes文件删除之后显示:找不到或无法加载主类解决方案
- spring @RequestBody The request sent by the client was syntactically incorrect.
- Java线程:概念与原理
- 用JAVA(Myeclipse)实现可视化日历
- myeclipse常用快捷键总结
- java设计模式——结构型之组合模式
- Eclipse IDE FOR JAVA常见错误
- java枚举的使用
- spring4整合quartz2定时器启动报:batch acquisition of 0 triggers
- Spring使用util Schema
- Java 信号量 Semaphore 介绍
- JDK8新特性面试
- JDK8新特性面试
- Java工程师成神之路
- Java编程入门
- Java笔记基础