Java NIO Chapter1 Learning Tips

1.java.io效率低的原因
But in most cases, Java applications have not truly been I/O bound in the sense that the operating system couldn't shuttle data fast enough to keep them busy. Instead, the JVMs have not been doing I/O efficiently. There's an impedance mismatch between the operating system and the Java stream-based I/O model. The operating system wants to move data in large chunks (buffers), often with the assistance of hardware Direct Memory Access (DMA). The I/O classes of the JVM like to operate on small pieces — single bytes, or lines of text. This means that the operating system delivers buffers full of
data that the stream classes of java.io spend a lot of time breaking down into little pieces, often copying each piece between several layers of objects. The operating system wants to deliver data by the truckload. The java.io classes want to process data by the shovelful. NIO makes it easier to back the truck right up to where you can make direct use of the data (a ByteBuffer object).

2.NIO中的解决方案概况
The java.nio package provides new abstractions to address this problem. The Channel and Selector classes in particular provide generic APIs to I/O services that were not reachable prior to JDK 1.4. The TANSTAAFL principle still applies: you won't be able to access every feature of every operating system, but these new classes provide a powerful new framework that encompasses the high-performance I/O features commonly available on commercial operating systems today. Additionally, a new Service Provider Interface (SPI) is provided in java.nio.channels.spi that allows you to plug in new types of channels and selectors without violating compliance with the specifications.
TANSTAAFL principle----------- There Ain't No Such Thing As A Free Lunch.

3.FileIO
In older operating systems, this usually meant issuing a command directly to the disk driver to read the needed disk sectors. But in modern, paged operating systems, the filesystem takes advantage of demand paging to bring data into memory.

3.1FileLocking
While the name "file locking" implies locking an entire file (and that is often done), locking is usually available at a finer-grained level. File regions are usually locked, with granularity down to the byte level. Locks are associated with a particular file, beginning at a specific byte location within that file and running for a specific range of bytes. This is
important because it allows many processes to coordinate access to specific areas of a file without impeding other processes working elsewhere in the file.
File locks are either advisory or mandatory.The former is used in the Most Unix and Unixlike System, while the latter is used in the Microsoft Operating System.

时间: 2024-10-04 00:04:45

Java NIO Chapter1 Learning Tips的相关文章

JAVA NIO存在的问题

    JAVA 包含最新的版本JDK1.8的NIO存在一些问题,这些问题需要在编写NIO程序时要格外关注: NIO跨平台和兼容性问题     NIO是底层API,它的实现依赖于操作系统针对IO操作的APIs. 所以JAVA能在所有操作系统上实现统一的接口,并用一致的行为来操作IO是很伟大的.     使用NIO会经常发现代码在Linux上正常运行,但在Windows上就会出现问题.所以编写程序,特别是NIO程序,需要在程序支持的所有操作系统上进行功能测试,否则你可能会碰到一些莫明的问题.   

介绍一些 Java NIO 的学习文章,以方便各位的学习!

NIO 入门: http://www-900.ibm.com/developerWorks/cn/cnonlinetutorial.nsf/gethtml?OpenAgent&url=/developerWorks/cn/education/java/j-nio/tutorial/index.html Java NIO原理和使用 : http://www.jdon.com/concurrent/nio%D4%AD%C0%ED%D3%A6%D3%C3.htm Merlin 给 Java 平台带来了

Java NIO API详解

详解 在JDK 1.4以前,Java的IO操作集中在java.io这个包中,是基于流的同步(blocking)API.对于大多数应用来说,这样的API使用很方便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO.从JDK 1.4起,NIO API作为一个基于缓冲区,并能提供异步(non-blocking)IO操作的API被引入.本文对其进行深入的介绍. NIO API主要集中在java.nio和它的subpackages中: java.nio 定义了Buff

基于java nio的memcached客户端

1.xmemcached是什么? xmemcached是基于java nio实现的memcached客户端API. 实际上是基于我实现的一个简单nio框架 http://code.google.com/p/yanf4j/的基础上实现的(目前是基于yanf4j 0.52),核心代码不超过1000行,序列化机制直接挪用spymemcached的Transcoder. 性能方面,在读写简单类型上比之spymemcached还是有差距,在读写比较大的对象(如集合)有效率优势. 当前0.50-beta版本

java nio 如何实现 阻塞读 不阻塞写

问题描述 java nio 如何实现 阻塞读 不阻塞写 java nio 如何实现 阻塞读 不阻塞写java nio 如何实现 阻塞读 不阻塞写 解决方案 java NIO 及 阻塞和非阻塞IO 解决方案二: 用selector可以实现不

java nio为什么会在Selector.select()函数一直等待

问题描述 java nio为什么会在Selector.select()函数一直等待 while (true) { // 选择一组键 selector.select(); // 返回此选择器的已选择键集 Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); // 遍历对应的 SelectionKey 处

java NIO

这次是关于java nio,有一些重复的发的地方.本文中的源代码可以在此处下载,下载链接为:http://115.com/file/cltlj10i#nio-src.zip 本文简介:  JDK 1.4 中引入的新输入输出 (NIO) 库在标准 Java 代码中提供了高速的.面向块的 I/O.本实用教程从高级概念到底层的编程细节,非常详细地介绍了 NIO 库.您将学到诸如缓冲区和通道这样的关键 I/O 元素的知识,并考察更新后的库中的标准 I/O 是如何工作的.您还将了解只能通过 NIO 来完成

理解Java NIO

基础概念 • 缓冲区操作 缓冲区及操作是所有I/O的基础,进程执行I/O操作,归结起来就是向操作系统发出请求,让它要么把缓冲区里的数据排干(写),要么把缓冲区填满(读).如下图 • 内核空间.用户空间  上图简单描述了数据从磁盘到用户进程的内存区域移动的过程,其间涉及到了内核空间与用户空间.这两个空间有什么区别呢?  用户空间就是常规进程(如JVM)所在区域,用户空间是非特权区域,如不能直接访问硬件设备.内核空间是操作系统所在区域,那肯定是有特权啦,如能与设备控制器通讯,控制用户区域的进程运行状

Java NIO系列教程(十二) Java NIO与IO

原文地址:http://tutorials.jenkov.com/java-nio/nio-vs-io.html 作者:Jakob Jenkov   译者:郭蕾    校对:方腾飞 当学习了Java NIO和IO的API后,一个问题马上涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们如何影响您的代码设计. Java NIO和IO的主要区别 下表总结了Java NIO和IO之间的主要差别,我会更详细地描述表中每部分