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.pswp.cn/news/392107.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392107.shtml
英文地址,請注明出處:http://en.pswp.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…