Java NIO vs. IO

【GlobalMapper精品教程】001:GlobalMapper23+24+25 Pro-x64中文汉化版安装教程(附软件包下载) Global Mapper 23+V24+V25中文汉化版安装教程及免费下载地址,是一款非常专业的地图绘制软件,功能非常的丰富,内置距离和面积计算,包括光栅混合、对比度调节、海拔高度查询、视线计算以及一些高级功能,通过地表数据观察分水岭、对3Dpoint数据转换为三角多边形和网格化等设置。本文讲解GlobalMapper23.1 Pro-x64安装过程。 阅读详情

http://tutorials.jenkov.com/java-nio/nio-vs-io.html

 

When studying both the Java NIO and IO API's, a question quickly pops into mind:

When should I use IO and when should I use NIO?

In this text I will try to shed some light on the differences between Java NIO and IO, their use cases, and how they affect the design of your code.


 

Main Differences of Java NIO and IO

The table below summarizes the main differences between Java NIO and IO. I will get into more detail about each difference in the sections following the table.

IONIO
Stream orientedBuffer oriented
Blocking IONon blocking IO
 Selectors

Stream Oriented vs. Buffer Oriented

The first big difference between Java NIO and IO is that IO is stream oriented, where NIO is buffer oriented. So, what does that mean?

Java IO being stream oriented means that you read one or more bytes at a time, from a stream. What you do with the read bytes is up to you. They are not cached anywhere. Furthermore, you cannot move forth and back in the data in a stream. If you need to move forth and back in the data read from a stream, you will need to cache it in a buffer first.

Java NIO's buffer oriented approach is slightly different. Data is read into a buffer from which it is later processed. You can move forth and back in the buffer as you need to. This gives you a bit more flexibility during processing. However, you also need to check if the buffer contains all the data you need in order to fully process it. And, you need to make sure that when reading more data into the buffer, you do not overwrite data in the buffer you have not yet processed.


 

Blocking vs. Non-blocking IO

Java IO's various streams are blocking. That means, that when a thread invokes aread() orwrite(), that thread is blocked until there is some data to read, or the data is fully written. The thread can do nothing else in the meantime.

Java NIO's non-blocking mode enables a thread to request reading data from a channel, and only get what is currently available, or nothing at all, if no data is currently available. Rather than remain blocked until data becomes available for reading, the thread can go on with something else.

The same is true for non-blocking writing. A thread can request that some data be written to a channel, but not wait for it to be fully written. The thread can then go on and do something else in the mean time.

What threads spend their idle time on when not blocked in IO calls, is usually performing IO on other channels in the meantime. That is, a single thread can now manage multiple channels of input and output.


 

Selectors

Java NIO's selectors allow a single thread to monitor multiple channels of input. You can register multiple channels with a selector, then use a single thread to "select" the channels that have input available for processing, or select the channels that are ready for writing. This selector mechanism makes it easy for a single thread to manage multiple channels.


 

How NIO and IO Influences Application Design

Whether you choose NIO or IO as your IO toolkit may impact the following aspects of your application design:

  1. The API calls to the NIO or IO classes.
  2. The processing of data.
  3. The number of thread used to process the data.


 

The API Calls

Of course the API calls when using NIO look different than when using IO. This is no surprise. Rather than just read the data byte for byte from e.g. anInputStream, the data must first be read into a buffer, and then be processed from there.


 

The Processing of Data

The processing of the data is also affected when using a pure NIO design, vs. an IO design.

In an IO design you read the data byte for byte from an InputStream or aReader. Imagine you were processing a stream of line based textual data. For instance:

Name: Anna
Age: 25
Email: anna@mailserver.com
Phone: 1234567890

This stream of text lines could be processed like this:

InputStream input = ... ; // get the InputStream from the client socket

BufferedReader reader = new BufferedReader(new InputStreamReader(input));

String nameLine   = reader.readLine();
String ageLine    = reader.readLine();
String emailLine  = reader.readLine();
String phoneLine  = reader.readLine();

Notice how the processing state is determined by how far the program has executed. In other words, once the firstreader.readLine() method returns, you know for sure that a full line of text has been read. ThereadLine() blocks until a full line is read, that's why. You also know that this line contains the name. Similarly, when the secondreadLine() call returns, you know that this line contains the age etc.

As you can see, the program progresses only when there is new data to read, and for each step you know what that data is. Once the executing thread have progressed past reading a certain piece of data in the code, the thread is not going backwards in the data (mostly not). This principle is also illustrated in this diagram:

Java IO: Reading data from a blocking stream.
Java IO: Reading data from a blocking stream.

A NIO implementation would look different. Here is a simplified example:

ByteBuffer buffer = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buffer);

Notice the second line which reads bytes from the channel into the ByteBuffer. When that method call returns you don't know if all the data you need is inside the buffer. All you know is that the buffer contains some bytes. This makes processing somewhat harder.

Imagine if, after the first read(buffer) call, that all what was read into the buffer was half a line. For instance, "Name: An". Can you process that data? Not really. You need to wait until at leas a full line of data has been into the buffer, before it makes sense to process any of the data at all.

So how do you know if the buffer contains enough data for it to make sense to be processed? Well, you don't. The only way to find out, is to look at the data in the buffer. The result is, that you may have to inspect the data in the buffer several times before you know if all the data is inthere. This is both inefficient, and can become messy in terms of program design. For instance:

ByteBuffer buffer = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buffer);

while(! bufferFull(bytesRead) ) {
    bytesRead = inChannel.read(buffer);
}

The bufferFull() method has to keep track of how much data is read into the buffer, and return eithertrue orfalse, depending on whether the buffer is full. In other words, if the buffer is ready for processing, it is considered full.

The bufferFull() method scans through the buffer, but must leave the buffer in the same state as before thebufferFull() method was called. If not, the next data read into the buffer might not be read in at the correct location. This is not impossible, but it is yet another issue to watch out for.

If the buffer is full, it can be processed. If it is not full, you might be able to partially process whatever data is there, if that makes sense in your particular case. In many cases it doesn't.

The is-data-in-buffer-ready loop is illustrated in this diagram:

Java NIO: Reading data from a channel until all needed data is in buffer.
Java NIO: Reading data from a channel until all needed data is in buffer.


 

Summary

NIO allows you to manage multiple channels (network connections or files) using only a single (or few) threads, but the cost is that parsing the data might be somewhat more complicated than when reading data from a blocking stream.

If you need to manage thousands of open connections simultanously, which each only send a little data, for instance a chat server, implementing the server in NIO is probably an advantage. Similarly, if you need to keep a lot of open connections to other computers, e.g. in a P2P network, using a single thread to manage all of your outbound connections might be an advantage. This one thread, multiple connections design is illustrated in this diagram:

Java NIO: A single thread managing multiple connections.
Java NIO: A single thread managing multiple connections.

If you have fewer connections with very high bandwidth, sending a lot of data at a time, perhaps a classic IO server implementation might be the best fit. This diagram illustrates a classic IO server design:

Java IO: A classic IO server design - one connection handled by one thread.
Java IO: A classic IO server design - one connection handled by one thread.


 



 

论文复现5:UMI Universal Manipulation Interface UMI 采用手持式夹具,加上精心的界面设计,可实现便携式、低成本和信息丰富的数据收集,以进行具有挑战性的双手和动态操作演示。配备这些功能后,UMI 框架解锁了新的机器人操作功能,只需更改每个任务的训练数据,即可实现零样本可推广的动态、双手、精确和长视野行为。我们通过全面的现实世界实验展示了 UMI 的多功能性和有效性,其中通过 UMI Zeroshot 学习的策略在对不同的人类演示进行训练时可以推广到新的环境和对象。项目主页:https://umi-gripper.github.io/ 阅读详情

相关推荐

IsaacLab 从入门到精通(一) 环境配置和依赖安装

本系列文章将针对机器人操作任务中的强化学习平台IsaacLab提供较为全面的教程

DejaWu33的博客 1万+

WINDOWS网络编程接口

SOCKETS规范及应用-WINDOWS网络编程接口 Windows Sockets 规范及应用 -Windows网络编程接口 ****************************************************************** 版权信息 本书作者保留所有版权。禁止任何商业性的转载或复制。非赢利性质的转载和复制不得修改文章内容,并请保留此段文字。 Cop

有趣的人生 一半是山川湖海 一半是初心情怀 1万+

USB2.0信号完整性测试之眼图

名称线缆颜色引脚描述红色绿色白色GND黑色。

qq_53766840的博客 3302

java NIO vs IO

Java NIO vs. IO 来自:http://hi.baidu.com/michaleyz/item/9b4e04012694693df2eafc52 当学习了JavaNIO和IO的API后,一个问题很快进入心中: 我应该在何时使用IO,何时使用NIO呢? 在本文中,我会尽量清晰地阐明JavaN...

dgit3228的博客 158

Tushare使用教程

@Tushare简易教程 Python财经数据接口Tushare 最近在看量化分析有关的东西,然后第一步先了解的就是如何获取股票交易数据。发现大部分教程都是关于Tushare的,于是在这里简单的介绍一下Tushare的用法吧。目前tushare有免费版本和升级的需要付费的Pro版本。大多数教程写的都是免费版本,但是pro版本提供的数据更多,但是由于本人入坑还不深,暂时还是用的免费版本,但是每次使用都收到提示,“本接口即将停止更新,请尽快使用Pro版接口:https://tushare.pro/documen

kuangzhi9124的博客 2万+

psasp和matlab,PSASP与Matlab/SimPowerSystems联合仿真接口方法研究

[1]李亚楼,吴中习.Matlab模型接入PSASP暂态稳定计算模块的方法[J].电网技术,2008,32(19):31-36.Li Yalou,Wu Zhongxi.An Approach to Interface Matlab Model with PSASP Transient Stability Module [J].Power System Technology,2008,32(19):...

weixin_39636245的博客 1087

【Java NIO 简例】NIO vs IO

原文:《Java NIO vs. IO》 当学习Java NIO 和 IO 的API时,很快会遇到一个问题: 什么时候用IO,什么时候用NIO? 我会尝试在本文提供一些关于Java NIO 与 IO 的不同点、使用案例、及它们将如何影响代码设计方面的见解。 NIO 与 IO 的主要不同 下表总结了Java NIO 和 IO 的主要不同。我会在后文详细论述每一个不同点。 ...

一个被IT搞的 205

Pspice安装教程:Windows系统完整指南(含环境变量设置)

详细讲解在Windows系统中完成Pspice安装的全过程,涵盖安装步骤与关键的环境变量设置,帮助用户顺利运行仿真工具。适合初学者快速上手pspice安装教程中的核心环节。

weixin_42230607的博客 1186

【翻译】Java IO VS NIO

原文地址:https://howtodoinjava.com/java/io/difference-between-standard-io-and-nio/ 简介 NIO JDK 1.4引入了一种新的输入/输出库 NIO(New IO)。 NIO提供标准Java代码中的高速,面向块的I/O,通过定义用于保存数据的类,并通过以块为单位处理该数据。NIO以原始I/O包不能使用本机代码(如底层C++)的...

郝伟老师的博客——大数据、并行计算与人工智能时代 883

别再手动拷贝文件了!用Keil MDK的Pack Installer一键搞定STM32上的RT-Thread Nano移植

本文详细介绍了如何使用Keil MDK的Pack Installer一键快速移植RT-Thread Nano到STM32项目,告别繁琐的手动拷贝和配置过程。通过自动化版本管理和依赖处理,移植时间从47分钟缩短至3分钟,大幅提升开发效率。文章还提供了实战步骤、高级配置技巧和常见问题解决方案,帮助开发者轻松实现RTOS集成。

weixin_29194817的博客 369

【ARM Trace32(劳特巴赫) 使用介绍 1.3 - ARM 系统调试中常见的挑战】

每一个独立的gust os或者一个OS 内部里面美格独立的进程它使用虚拟地址是可能是重叠的,比如linux中每个用户进程它的运行地址(虚拟地址)都是0x0,这时如果想去同时调试所有进程,那么必须要有个办法把这些进程区分开来,trace32 就在虚拟地址前面加前缀的方法,这个前缀称为space id, 这个space id 实际上就是processor id或者是task id, 这样就可以把一个os内部的不同的进程区分开来。如果工具和内核中的连接是可以建立起来的,那么接下来就是调试工具使用的问题了。

CodingCos的博客 1965

JAVA NIO vs IO

Java NIO和IO的主要区别 (面向Steam、阻塞IO)IO VS NIO(面向Buffer,非阻塞IO,Selectors) 面向Stream和面向Buffer Java NIO和IO之间最大的区别是IO是面向流(Stream)的,NIO是面向块(buffer)的,所以,这意味着什么? 面向流意味着从流中一次可以读取一个或多个字节,拿到读取的这些做什么你说了算,这...

Don‘t 154

UDS自动化测试-Service 0x27(CAPL调用dll实现key计算)

相信读者基于Diagnostic/ISO TP Confighration中cdd、dll文件的测试并不陌生,本文基于其的基础上,介绍在capl中调用内置的函数diagGenerateKeyFromSeed实现27服务解锁,从而进行诊断自动化测试。本文基于4个byte的Seed、16个byte的Key的安全访问,实现27服务的自动化测试。需要加载项目使用的cdd文件,如果没有的情况下可以使用canoe例程中默认的cdd文件。

诊断协议那些事儿's blog 2055

java+io+nio+区别,Java IO vs NIO,真正的区别是什么?

我非常喜欢Java NIO,我真的想将Java NIO应用到我当前的系统中,但是当我创建这些示例应用程序来比较Java IO和NIO时,它让我非常失望.这是我的2个样本(我没有把所有的源代码)Java IOpublic class BlockingServerClient {private static final Logger log = Logger.getLogger(BlockingSer...

weixin_30921875的博客 180

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Java IO vs NIO

Java.nio vs Java.io By Nino Guarnacci-Oracle on Jun 18, 2009 ……… posted by Davide Pisano This document is not a Java.io or a Java.nio manual, or a technical document about Java.io and

fqwgc8的博客 498

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

JAVA SOCKET IO VS NIO

When studying both the Java NIO and IO API's, a question quickly pops into mind: When should I use IO and when should I use NIO? In this text I will try to shed some light on the differences between

smithdoudou88的专栏 809

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Java.nio vs Java.io

Java.nio vs Java.io

太阳火神的美丽人生 2641

Java Nio 十四、Java NIO vs. IO

最后更新时间:2014-06-23 翻译地址:http://tutorials.jenkov.com/java-nio/nio-vs-io.html

jianjun200607的专栏 3188

java nio io博客_Java NIO vs. IO

当学习了Java NIO和IO的API后,一个问题很快进入心中:我应该在何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地阐明Java NIO和IO的差异、它们的用例,以及它们如何影响您的代码设计。下表总结了Java NIO和IO之间的主要差别,我会更详细地描述表中每部分的差异。IONIO面向流 面向缓冲阻塞IO 非阻塞IO选择器1.面向流与面向缓...

weixin_39957934的博客 58

Java NIO VS IO

原文地址:http://tutorials.jenkov.com/java-nio/nio-vs-io.html 作者:Jakob Jenkov   译者:郭蕾    校对:方腾飞 当学习了Java NIO和IO的API后,一个问题马上涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异、它们的使用场景,以及它们如何影响您的代

This is bill的专属博客 877

Java NIO vs IO

NIO :http://tutorials.jenkov.com/java-nio/index.html IO:http://tutorials.jenkov.com/java-io/index.html 转载于:https://www.cnblogs.com/SaraMoring/p/5720277.html

dielishang0645的博客 91
上一篇: JAVA 常用算法总结
下一篇: Android分别使用HTTP协议和TCP协议实现上传文件 .
什么 IT
博客等级 码龄20年 65粉丝 8原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值