简体   繁体   English

使用java.io包的Java Reactor模式

[英]Java Reactor Pattern with java.io Package

I see in several application source like Minecraft and JIrcs they both use java.io to implement Reactor Plugin (if I'm not wrong) and also in this article . 我在几个应用程序源中看到,比如Minecraft和JIrcs,他们都使用java.io来实现Reactor Plugin(如果我没有错),也在本文中 So, what is the difference between java.io and java.nio when implementing Reactor Pattern? 那么,实现Reactor Pattern时java.io和java.nio有什么区别? I mean, like performance advantage, process efficiency etc and where i can get good tutorial if you think java.io is the good solution to implement Reactor Pattern (since google give me tons of java.nio tuts not java.io as i want) 我的意思是,像性能优势,流程效率等,如果你认为java.io是实现Reactor Pattern的好解决方案,我可以获得良好的教程(因为谷歌给了我大量的java.nio tuts而不是我想要的java.io)

I hope you can get a conclusion with the below information taken from the book at page no.42 我希望你能从第42页的书中得到以下信息得出结论

java.io.* classes use the decorator design pattern . java.io. *类使用装饰器设计模式 The decorator design pattern attaches responsibilities to objects at runtime. 装饰器设计模式在运行时将责任附加到对象。 Decorators are more flexible than inheritance because the inheritance attaches responsibility to classes at compile time . 装饰器比继承更灵活,因为继承在编译时将责任附加到类 The java.io.* classes use the decorator pattern to construct different combinations of behaviour at runtime based on some basic classes. java.io. *类使用装饰器模式在运行时基于一些基本类构造不同的行为组合。

and 43. 和43。

Java has no long been suited for developing programs that perform a lot of I/O operations. Java不再适用于开发执行大量I / O操作的程序。 Furthermore, commonly needed tasks such as file locking, non-blocking and asynchronous I/O operations and ability to map file to memory were not available. 此外,通常需要的任务(如文件锁定,非阻塞和异步I / O操作以及将文件映射到内存的功能)不可用。 Non-blocking I/O operations were achieved through work around such as multithreading or using JNI. 通过多线程或使用JNI等解决方案实现了非阻塞I / O操作。 The New I/O API (aka NIO ) in J2SE 1/4 has changed this situation. J2SE 1/4中的新I / O API (又名NIO )改变了这种情况。 A server's availability to handle several client requests effectively depends on how it uses I/O streams. 服务器有效处理多个客户端请求的可用性取决于它如何使用I / O流。 When a server has to handle hundreds of clients simultaneously, it must be able to use I/O services concurrenty, one way to cater for this scenario in Java is to use threads but having almost one-to-one ratio of threads (100 clients will have 100 threads) is prone to anormous thread overhead and can result in performance and scalability problems due to consumtion of memory stacks (ie each thread has its stack, Refer Q34 , Q42 in Java section) and CPU context switching (ie switching between threads as opposed to doing real computation.). 当服务器必须同时处理数百个客户端时,它必须能够同时使用I / O服务,在Java中满足这种情况的一种方法是使用线程但具有几乎一对一的线程比率(100个客户端)将有100个线程)容易出现巨大的线程开销,并且由于内存堆栈的消耗(即每个线程都有其堆栈,参见Q34Q42 in Java部分)和CPU上下文切换 (即线程之间的切换 ), 可能导致性能和可伸缩性问题而不是做真正的计算。 To overcome this problem, a new set of non-blocking I/O classes have been introduced to the Java platform in java.nio package. 为了解决这个问题,在java.nio包中向Java平台引入了一组新的非阻塞I / O类。 The non-blocking I/O mechanism is built around Selectors and channels. 非阻塞I / O机制围绕选择器和通道构建。 Channels , Buffers and Selectors are the core of the NIO. 通道缓冲器选择器是NIO的核心。

and read more . 阅读更多 Here are some reference links which provide Java IO vs Java NIO : Java IO Faster Than NIO – Old is New Again! 以下是一些提供Java IO与Java NIO的参考链接: Java IO比NIO更快 - 旧的再次出现! , Java IO vs Java NIO and IO vs. NIO – Interruptions, Timeouts and Buffers . Java IO与Java NIOIO与NIO - 中断,超时和缓冲区

It is important to understand the differences between blocking and non-blocking I/O. 了解阻塞和非阻塞I / O之间的区别非常重要。 The java.io package does NOT support non-blocking I/O so it won't scale for a large number of connections. java.io包不支持非阻塞I / O,因此它不会扩展为大量连接。 Moreover, with blocking I/O you can only resort to the terrible one-client-to-one-thread model which is completely unsuitable for low-latency systems. 此外,通过阻塞I / O,您只能使用可怕的单客户端到单线程模型 ,这完全不适合低延迟系统。 I suggest you take a look on this article written by me about an asynchronous network I/O library that implements the reactor pattern, so you can understand how non-blocking plays a key role in high-performance systems. 我建议你看一下我写的关于实现反应器模式的异步网络I / O库的这篇文章 ,这样你就可以理解非阻塞在高性能系统中如何发挥关键作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM