java异步io_Java中的异步IO与异步请求处理

java异步io

In this article, I am trying to explain the difference between Async-IO and Async-Request processing in the HTTP request in the Java world.

在本文中,我试图解释Java世界中HTTP请求中Async-IO和Async-Request处理之间的区别。

In the pre-Java 1.4 world, Java provides an API to send/receive data over the network socket. The original authors of JVM mapped this API behavior to OS socket API, almost one to one.

在Java 1.4之前的版本中,Java提供了一个API,用于通过网络套接字发送/接收数据。 JVM的原始作者将此API行为几乎一对一地映射到OS套接字API。

So, what is the OS socket behaviour? OS provides Socket programming api, which has send/recv blocking call. Since java is just a process running on top of linux(OS), hence this java program has to use this blocking api provided by OS.

那么,操作系统套接字的行为是什么? OS提供了Socket编程api ,该API具有send / recv 阻止调用 。 由于Java只是在linux(OS)之上运行的进程,因此该Java程序必须使用OS提供的此阻塞api。

The world was happy and java developers started using the API to send/receive the data. But they had to keep one java thread for every socket(client).

全世界都很高兴,Java开发人员开始使用API​​发送/接收数据。 但是他们必须为每个套接字(客户端)保留一个Java线程。

Everybody was writing their own flavor of HTTP servers. Code sharing was becoming hard, the java world demanded a standardization.Enters the java servlet Spec.

每个人都在编写自己的HTTP服务器。 代码共享变得越来越困难,Java世界要求实现标准化。 输入Java Servlet规范。

Before moving on lets define few terms:

在继续之前,让我们先定义几个术语:

Java Server Developer: People who are using the java socket api and implementing http protocol like tomcat.

Java Server Developer :正在使用Java套接字api并实现诸如tomcat之类的http协议的人们。

java Application Developer: People who are building buisness application on top of tomcat.

Java应用程序开发人员:在Tomcat之上构建商务应用程序的人们。

GETTING BACK NOW

现在回来

Once the java servlet spec entered the world, it said:

Java Servlet规范进入世界后,它说:

Dear java server developers, please provide a method like below:

尊敬的Java服务器开发人员,请提供以下方法:

doGet(inputReq, OutPutRes)

so that java application developer can implement doGet and they can write their business logic. Once “application developer” wants to send the response, he can call OutPutRes.write().

这样Java应用程序开发人员就可以实现doGet并编写自己的业务逻辑。 一旦“应用程序开发人员”想要发送response ,他就可以调用OutPutRes.write().

A thing to Note:Since socket api is blocking, hence OutPutRes.write() is also blocking. Also, the additional limitation was that the response object is committed on doGet method exit.

注意事项:由于套接字api被阻塞,因此OutPutRes.write()也被阻塞。 另外,另一个限制是响应对象在doGet方法退出时提交。

Due to these limitations, people had to use one thread for processing one request.

由于这些限制,人们不得不使用一个线程来处理一个请求。

Time passed and the internet took over the world. one Thread per Request started to show limitations.

时间流逝,互联网占领了世界。 每个请求一个线程开始显示限制。

问题一: (Problem 1:)

The thread-per-request model fails when there are long pauses during the processing of each request.

当每个请求的处理过程中出现长时间的停顿时,每个请求线程模型将失败。

For Example: fetching data from sub-service take long time.

例如:从子服务中获取数据需要很长时间。

Under such a situation, the thread is mostly sitting idle and JVM can run out of thread easily.

在这种情况下,线程通常处于空闲状态,JVM可以很容易地用完线程。

问题2: (Problem 2:)

Things got even worse with http1.1 persistent connection. As with persistent connection, the underlying TCP connection will be kept alive and the server has to block one thread per connection.

使用http1.1持久连接,情况变得更糟。 与持久连接一样,基础TCP连接将保持活动状态,并且服务器必须为每个连接阻止一个线程。

But why does the server have to block one thread per connection?

但是,为什么服务器必须为每个连接阻塞一个线程?

But why does the server have to block one thread per connection?Since OS provides a blocking socket Recv api, the jvm has to call the OS blocking Recv method in order to listen for more requests on same tcp connection from the client.

但是,为什么服务器必须为每个连接阻塞一个线程? 由于OS提供了阻塞套接字Recv api,因此jvm必须调用OS阻塞Recv方法,以便在来自客户端的同一tcp连接上侦听更多请求。

世界要求解决方案! (The world demanded a solution!)

The First Solution came from the creator of JVM. They introduced NIO(ASYNC-IO). Nio is the non-blocking API for sending/receiving data over socket.

第一个解决方案来自JVM的创建者。 他们介绍了NIO( ASYNC-IO ) 。 Nio是用于通过套接字发送/接收数据的非阻塞API。

Some background: the OS along with blocking socket api also provides a non-blocking version of the socket api.

一些背景: 操作系统以及阻止套接字api也提供了套接字api的非阻止版本。

But how does the OS provide that .. Does it fork a thread internally and that thread gets blocked???

但是,操作系统如何提供该功能呢?它是否在内部派生了一个线程并且该线程被阻塞了?

The ANSWER is no… the OS instruct the hardware to interupt when there is data to read or write.

答案不是……当有数据需要读取或写入时,操作系统会指示硬件中断。

NIO allowed the java server developer” to tackle problem 2 of blocking one thread per TCP connection. With NIO being an HTTP persistent connection, the thread does not require it to block on recv call. Instead, it can now process it only when there is data to be processed. This allowed one thread to monitor/handle a large number of persistent connections.

NIO允许java服务器开发人员” 解决问题2每个TCP连接阻塞一个线程 。 由于NIO是HTTP持久连接,因此该线程不需要它在recv调用时阻塞。 相反,它现在只能在有要处理的数据时进行处理。 这允许一个线程监视/处理大量持久连接。

The Second Solution came from servlet spec. Servlet Spec got an upgrade and they introduced async support (Async Request Processing).

第二个解决方案来自servlet规范。 Servlet Spec进行了升级,并引入了异步支持 (异步请求处理)。

AsyncContext acontext = req.startAsync();

IMPORTANT: This upgrade removed the limitation of committing the response object on doGet method completion.

重要说明: 此升级消除了在doGet方法完成时提交响应对象的限制。

This allowed the “Java Application Developer” to tackle Problem 1, by offloading work to background threads. Now instead of keeping the thread waiting during the long pause, the thread can be used to handle other requests.

这样,“ Java应用程序开发人员”就可以通过将工作卸载到后台线程来解决问题1 。 现在,可以使线程不必处理长时间的暂停,而可以使用该线程来处理其他请求。

结论: (CONCLUSION:)

Async-IO in java is basically using the non-blocking version on OS socket API.

Java中的Async-IO基本上在OS套接字API上使用非阻塞版本。

Async request processing is basically the servlet spec standardization of how to process more requests with one thread.

异步请求处理基本上是servlet规范中的一个规范,该规范规定了如何通过一个线程处理更多请求。

参考资料: (REFERENCES:)

https://www.scottklement.com/rpg/socktut/tutorial.pdfhttps://stackoverflow.com/questions/15217524/what-is-the-difference-between-thread-per-connection-vs-thread-per-request

https://www.scottklement.com/rpg/socktut/tutorial.pd f https://stackoverflow.com/questions/15217524/what-is-the-difference-between-thread-per-connection-vs-thread-每个请求

Motivation of article: Team Learning/Knowledge Sharing

文章动机:团队学习/知识共享

翻译自: https://www.freecodecamp.org/news/java-async-io-async-request-processing-in-http-request-1a04f395d8c7/

java异步io

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392107.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

异常检测机器学习_使用机器学习检测异常

异常检测机器学习什么是异常检测? (What is Anomaly Detection?) The anomaly detection problem has been a problem that has been frequently explored in the field of machine learning and has become a classic problem. Anomalies are any unusual sequenc…

数据挖掘—BP神经网络(Java实现)

public class Test {public static void main(String args[]) throws Exception {ArrayList<ArrayList<Double>> alllist new ArrayList<ArrayList<Double>>(); // 存放所有数据ArrayList<String> outlist new ArrayList<String>(); // …

c语言掌握常用函数,c语言一些常用函数.pdf

c语言一些常用函数C 语言程序设计(常用函数说明)C 语言是 1972 年由美国的 Dennis Ritchie 设计发明的,并首次在 UNIX 操作系统的 DEC PDP-11 计算机上使用。它由早期的编程语言 BCPL(Basic Combind ProgrammingLanguage)发展演变而来。在 1970 年,AT&T 贝尔实验室的 Ken T…

高阶函数 - 函数节流

/*** 函数节流 - 限制函数被频繁调用* param {Function} fn [需要执行的函数]* param {[type]} interval [限制多长的时间再重复执行fn]*/var throttle function(fn, interval) {var __self fn,timer,firstTime true;return function() {var args arguments,__me…

[CareerCup] 8.7 Chat Server 聊天服务器

8.7 Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve? 这个简易的聊天服务器功能十分的有限&#xff0c;毕竟只是针对面试题的&…

react hooks使用_如何开始使用React Hooks:受控表格

react hooks使用by Kevin Okeh由Kevin Okeh 如何开始使用React Hooks&#xff1a;受控表格 (How to Get Started With React Hooks: Controlled Forms) React Hooks are a shiny new proposal that will allow you to write 90% cleaner React. According to Dan Abramov, Hoo…

特征工程tf-idf_特征工程-保留和删除的内容

特征工程tf-idfThe next step after exploring the patterns in data is feature engineering. Any operation performed on the features/columns which could help us in making a prediction from the data could be termed as Feature Engineering. This would include the…

c语言定义数组a10 指定各元素,C语言填空题.doc

C语言填空题.doc二、填空题1、C 语言只有 32 个关键字和 9 种控制语句。2、每个源程序有且只有一个 main 函数&#xff0c;系统总是从该函数开始执行 C 语言程序。 3、C 语言程序的注释可以出现在程序中的任何地方&#xff0c;它总是以 * 符号作为开始标记&#xff0c;以 */ 符…

猫狗队列

功能要求&#xff1a; 用户可以调用push方法将cat类或dog类的实例放入队列中;用户可以调用pollAll方法&#xff0c;将队列中所有的实例按照进队列的先后顺序依次弹出;用户可以调用pollDog方法&#xff0c;将队列中dog类的实例按照进队列的先后顺序依次弹出;用户可以调用pollCat…

如何使用HTML5,JavaScript和Bootstrap构建自定义文件上传器

by Prashant Yadav通过Prashant Yadav 如何使用HTML5&#xff0c;JavaScript和Bootstrap构建自定义文件上传器 (How to build a custom file uploader with HTML5, JavaScript, & Bootstrap) In this short article, we’ll learn how to create custom file uploader wit…

monkey测试===通过monkey测试检查app内存泄漏和cpu占用

最近一直在研究monkey测试。网上资料很多&#xff0c;但都是一个抄一个的。原创的很少 我把检查app内存泄漏的情况梳理一下&#xff1a; 参考资料&#xff1a; Monkey测试策略&#xff1a;https://testerhome.com/topics/597 Android Monkey测试详细介绍&#xff1a;http://www…

数据挖掘—主成分分析法降维和最小最大规范化

算法步骤:1)将原始数据按列组成n行m列矩阵X2)特征中心化。即每一维的数据都减去该维的均值&#xff0c;使每一维的均值都为03)求出协方差矩阵4)求出协方差矩阵的特征值及对应的特征向量5)将特征向量按对应的特征值大小从上往下按行排列成矩阵&#xff0c;取前k行组成矩阵p6)YPX…

用户使用说明c语言,(C语言使用指南.docx

(C语言使用指南Turbo C(V2.0)使用指南(本文的许多命令或方法同样适用于TC3) 在开始看本文以前&#xff0c;我先说明一下C语言的安装和使用中最应该注意的地方&#xff1a;许多网友在下载Turbo C 2.0和Turbo C 3.0后&#xff0c;向我问得最多的是在使用过程中碰到如下问题&…

三维空间两直线/线段最短距离、线段计算算法 【转】

https://segmentfault.com/a/1190000006111226d(ls,lt)|sj−tj||s0−t0(be−cd)u⃗ −(ae−bd)v⃗ ac−bd(ls,lt)|sj−tj||s0−t0(be−cd)u⃗ −(ae−bd)v⃗ ac−b2|具体实现代码如下&#xff08;C#实现&#xff09;&#xff1a; public bool IsEqual(double d1, double d2) { …

【慎思堂】之JS牛腩总结

一 JS基础 1-定义 Javascript是一种脚本语言/描述语言&#xff0c;是一种解释性语言。用于开发交互式web网页&#xff0c;使得网页和用户之间实现了一种实时性的、动态的、交互性的关系&#xff0c;使网页包含更多活跃的元素和更加精彩的内容。 主要用于&#xff1a;表单验证 …

vuejs 轮播_如何在VueJS中设计和构建轮播功能

vuejs 轮播by Fabian Hinsenkamp由Fabian Hinsenkamp设计 A carousel, slideshow, or slider — however you call it this class of UI — has become one of the core elements used in modern web development. Today, it’s almost impossible to find any Website or UI …

iOS绘圆形图-CGContextAddArc各参数说明

2019独角兽企业重金招聘Python工程师标准>>> 1.使用 UIGraphicsGetCurrentContext() 画圆 CGContextAddArc(<#CGContextRef _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFlo…

c语言中if和goto的用法,C语言中if和goto的用法.doc

C语言中if和goto的用法C语言中&#xff0c;if是一个条件语句&#xff0c;用法??if(条件表达式) 语句如果满足括号里面表达式&#xff0c;表示逻辑为真于是执行后面的语句&#xff0c;否则不执行(表达式为真则此表达式的值不为0&#xff0c;为假则为0&#xff0c;也就是说&…

数据挖掘—K-Means算法(Java实现)

算法描述 &#xff08;1&#xff09;任意选择k个数据对象作为初始聚类中心 &#xff08;2&#xff09;根据簇中对象的平均值&#xff0c;将每个对象赋给最类似的簇 &#xff08;3&#xff09;更新簇的平均值&#xff0c;即计算每个对象簇中对象的平均值 &#xff08;4&#xf…

自我价值感缺失的表现_不同类型的缺失价值观和应对方法

自我价值感缺失的表现Before handling the missing values, we must know what all possible types of it exists in the data science world. Basically there are 3 types to be found everywhere on the web, but in some of the core research papers there is one more ty…