nio

NIO与IO的流程梳理

放肆的年华 提交于 2020-01-12 20:02:16
IO与NIO详解----面试整理 在java种我们所说的IO与NIO其实是java对操作系统的IO模型的封装而产生的接口,是我们开发人员不必关注操作系统层面的知识。 同步: 同步就是发起一个调用后,被调用者未处理完请求之前,调用不返回。 异步: 异步就是发起一个调用后,立刻得到被调用者的回应表示已接收到请求,但是被调用者并没有返回结果,此时我们可以处理其他的请求,被调用者通常依靠事件,回调等机制来通知调用者其返回结果。 传统的BIO模型 一请求一应答,客户端发起socket连接,服务端通过serversocket进行连接。但是对于多个连接请求这样的模型就没法处理了这样衍生出来的就是多线程了。 多线程处理多对多 多个连接请求发送到服务端后,服务端每收到一个请求就开一个线程去为这个请求建立连接,这样就可以完成多连接请求的需求了。 **问题一:**为什么采用多线程,而不一个一个处理连接请求呢? 答:由于socket的accept、read、write方法都是阻塞的,如果一旦一个连接需要很长时间的读写,那么后续的连接就无法及时的响应了。 **问题二:**多线程的方式有什么问题,该如何解决? 答:采用多线程去解决这种场景的问题一旦请求量过大的时候就会无休止的创建线程,但线程资源很宝贵,在linux种进程的本质就是线程,所以一旦创建一个线程调用的是系统及的函数,开销很大

【译】Java NIO AsynchronousFileChannel

此生再无相见时 提交于 2020-01-12 16:24:35
在Java 7中NIO部分新增了AsynchronousFileChannel。AsynchronousFileChannel可以异步的读写文件了。下面教程介绍了它的用法。 创建一个AsynchronousFileChannel 通过它的静态方法open可以创建一个对象: Path path = Paths.get("data/test.xml"); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); 第一个参数是一个文件路径,它指向要访问的文件。 第二个参数是一个或多个打开的操作选项,它告诉AsynchronousFileChannel在底层对文件执行什么操作。上面的例子我们使用了StandardOpenOption.READ,这就表示文件打开是为了读取数据。 读数据 有两种方法。每种读取数据的方法都调用了AsynchronousFileChannel的read()方法之一。这两种读取数据的方法将在下面几节中讨论。 通过Future读数据 第一种是通过返回一个Future对象来读数据: Future<Integer> operation = fileChannel.read(buffer, 0); 第一个参数是一个buffer

Is ThreadLocal safe to use with Tomcat NIO Connector

一世执手 提交于 2020-01-12 07:47:11
问题 This just came to mind when testing the Tomcat NIO connector during my load tests. I make use of ThreadLocal's additionally I use Spring, which I know in several places it also makes use of it. Since the NIO connector does not have a thread per connection, I worry that it may result in very hard to find bugs if a ThreadLocal object was shared with another thread before it had been cleaned up. However, I assume that this is not an issue as it is not a documented warning that I could find, nor

阿里十年高端架构师总结:最大化Java NIO和NIO.2的五种方法

ぃ、小莉子 提交于 2020-01-11 23:23:17
JavaNIO的目的是改善Java平台上I / O密集型杂项的编程。十年后,许多Java程序员仍然不知道如何充分利用NIO,甚至更少的人意识到Java SE 7引入了更多新的输入/输出API(NIO.2)。在本教程中,您将找到五个简单的示例,这些示例演示了NIO和NIO.2软件包在常见Java编程场景中的优势。 NIO和NIO.2对Java平台的主要贡献是提高Java应用程序开发的核心领域之一中的性能:输入/输出处理。两种软件包都不是特别容易使用的,每个Java I / O场景都不需要新的Input / Output API。但是,正确使用Java NIO和NIO.2可以节省一些常见I / O操作所需的时间。这就是NIO和NIO.2的超级能力。 本文介绍了五种相对简单的方法来利用它们: 变更通知者(因为每个人都需要听众) 选择器帮助多路复用 渠道-承诺与现实 内存映射-至关重要 字符编码和搜索 NIO环境 已有 10年历史的增强功能仍然是Java 的New Input / Output包吗?原因是对于许多工作的Java程序员而言,基本的Java I / O操作已经足够了。大多数Java开发人员不具备学习NIO为我们的日常工作。而且,NIO不仅仅是性能套件。相反,它是与Java I / O相关的功能的异构集合。NIO通过“更接近Java程序”来提高Java应用程序的性能

Java NIO on Android: Attempting to READ/WRITE while connection is still pending?

大城市里の小女人 提交于 2020-01-11 12:11:55
问题 This question regards Java's NIO on Android (2.2, though I can build for higher APIs if necessary): After performing a SocketChannel connect() to a destination IP address, I register my channel for a READ operation. The problem is that when I try to perform a READ on the resulting selected key set, I receive NotYetConnectedException. While I can check the channel's status with isConnectionPending before I try to READ, I would ideally like to have READ keys be selected only when the connection

Java NIO on Android: Attempting to READ/WRITE while connection is still pending?

China☆狼群 提交于 2020-01-11 12:11:52
问题 This question regards Java's NIO on Android (2.2, though I can build for higher APIs if necessary): After performing a SocketChannel connect() to a destination IP address, I register my channel for a READ operation. The problem is that when I try to perform a READ on the resulting selected key set, I receive NotYetConnectedException. While I can check the channel's status with isConnectionPending before I try to READ, I would ideally like to have READ keys be selected only when the connection

Using Java nio to create a subdirectory and file

混江龙づ霸主 提交于 2020-01-10 17:31:23
问题 I'm creating a simple program that will try to read in "conf/conf.xml" from disk, but if this file or dir doesn't exist will instead create them. I can do this using the following code: // create subdirectory path Path confDir = Paths.get("./conf"); // create file-in-subdirectory path Path confFile = Paths.get("./conf/conf.xml"); // if the sub-directory doesn't exist then create it if (Files.notExists(confDir)) { try { Files.createDirectory(confDir); } catch (Exception e ) { e.printStackTrace

Using Java nio to create a subdirectory and file

血红的双手。 提交于 2020-01-10 17:30:08
问题 I'm creating a simple program that will try to read in "conf/conf.xml" from disk, but if this file or dir doesn't exist will instead create them. I can do this using the following code: // create subdirectory path Path confDir = Paths.get("./conf"); // create file-in-subdirectory path Path confFile = Paths.get("./conf/conf.xml"); // if the sub-directory doesn't exist then create it if (Files.notExists(confDir)) { try { Files.createDirectory(confDir); } catch (Exception e ) { e.printStackTrace

nio之postion,limit,capacity

狂风中的少年 提交于 2020-01-10 17:14:39
postion:当前可读可写的位置 capacity:缓冲区容量,初始化后,固定不变 limit:缓存区最小不可读写的区域的位置,缓冲区域limit索引以后的区域不可读写(含limit位置) 先初始化一个IntBuffer IntBuffer intBuffer = IntBuffer.allocate ( 10 ) ; 初始化完后,limit等于capacity,也就是指向缓冲区末尾的虚线框位置,postion指向缓冲区的第一个位置 接下来插入四个值 intBuffer.put ( 1 ) ; intBuffer.put ( 2 ) ; intBuffer.put ( 3 ) ; intBuffer.put ( 4 ) ; 此时我们从缓冲区读取一个数据 System.out.println ( intBuffer.get ( )) ; 执行完后,控制台打印出 0 ,和我们想象中不太一样,其实我们是想得到 1 ;在我们调用intBuff.get()方法时,它会返回postion位置的值;其实仔细的同学会发现,执行完get方法后,postion的值会+1操作;如果我们想从最开始的位置读取数据,如何操作? intBuffer.flip ( ) 执行完flip()方法后,postion指向了0位置,limit指向了5位置 limit = postion postion = 0

NIO

随声附和 提交于 2020-01-10 15:56:15
阻塞式I/O模型: 非阻塞式I/O模型: 在上图中NIO通过Selector 接受请求连接,如map.put("地址A“, accept),map.put("地址B“,read),map.put("地址C“,write) 当有请求read/write时,才开始建立Channel 建立请求和wirte 通道,并且通过Buffer获取数据块 NIO关键类: Selector Channel Buffer 来源: https://www.cnblogs.com/fanBlog/p/12176320.html