久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

單例 Bean 如何服務并發請求?

How does the singleton Bean serve the concurrent request?(單例 Bean 如何服務并發請求?)
本文介紹了單例 Bean 如何服務并發請求?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個關于單例 bean 如何詳細處理并發請求的問題.

I have a question regarding how singleton beans serve concurrent requests in detail.

我在 StackOverflow 上搜索過這個問題.這是一個示例來自stackoverflow的鏈接,但我只找到了高級細節.我想要關于單例 bean 如何處理并發請求以及系統處理器如何查看這些請求的完整詳細信息.

I have searched on StackOverflow regarding this question. This is a sample link from stackoverflow, but I found only high level details. I want full details on how a singleton bean serves concurrent requests and how the system processor will see those requests.

我在線研究了系統處理器中的并發請求處理.他們說處理器本身有一個調度器,調度器將決定處理哪個請求.

I have researched regarding concurrent request handling in the system processor online. They said the processor itself has a scheduler and that scheduler will decide which request gets processed.

好的.如果假設我有多個核心處理器,調度器如何處理并發請求?

Ok fine. If suppose I have more than one core processor, how does the scheduler handle concurrent requests?

誰能向我解釋一下單例 bean 如何在 JVM 和系統中為并發請求提供服務的分步過程?

Can anyone explain to me the step-by-step process on how a singleton bean will serve concurrent requests in the JVM and system?

讓我用一個具體的例子來解釋.我有一個像 Sports 這樣的課程:

Let me explain with a concrete example. I have a class like Sports:

class Sports {
    public void playFootball() {
    }

    public void playVolleyBall() {
    }
}

有兩個請求進來.第一個請求是在 Sports 類的已創建單例實例上執行 playFootball 方法.同時,另一個請求正在對 Sports 類的同一個已創建單例實例執行 playVolleyBall 方法.

Two requests come in. The first request is executing the playFootball method on the created singleton instance of class Sports. At the same time, another request is executing the playVolleyBall method on the same created singleton instance of class Sports.

單例實例怎么可能?

推薦答案

Saravan Kumar,

Saravan Kumar,

我了解您提出問題的動機.在我開始研究編譯器之前,我也有一個非常相似的想了解 Java 虛擬機的內部結構.

I understand the motivation behind your question. Before I started working on compilers, I also had a very similar wanting to know the internals of the Java Virtual Machine.

首先,我對你的問題印象深刻.為了解決您的問題,需要有幾點區別和理解.首先:單例模式,有時甚至稱為反模式,確保只有一個此類的實例可用于 JVM(Java 虛擬機).這意味著我們本質上是將全局狀態引入應用程序.我知道你明白這一點,但這只是一個澄清點.

First of all, I'm impressed by your question. There needs to be a couple of points of distinctions and understanding in order to solve your question. Firstly: A Singleton pattern, or sometimes even called an anti-pattern, ensures that there is only one instance of this class available to the JVM(Java Virtual Machine). This means we are essentially introducing a global state into an application. I know you understand this, but it is just a point of clarification.

現在是內部結構.

當我們創建一個類的實例時,我們正在創建一個駐留在 JVM 共享內存中的對象.現在,這些線程正在獨立執行對這些實例進行操作的代碼.每個線程都有一個工作內存,它保存所有線程之間共享的主內存中的數據.這是對您創建的 Singleton 對象的引用所在的位置.本質上,正在發生的事情是生成的代表您創建的單例對象的字節碼正在這些線程中的每一個線程上執行.

When we create an instance of a class, we are creating an object that is residing in JVM's shared memory. Now, these threads are independently executing code that operates on these instances. Each thread has a working memory, in which it keeps data from the main memory that are shared between all threads. This is where the reference to the Singleton object you have created resides. Essentially what is happening is that the bytecode which was generated and is representative of the singleton object you created is being executed on each one of these threads.

現在內部情況如下:

每個 JVM 線程都有一個私有 JVM 堆棧,與線程同時創建.現在,JVM 有一個在所有 JVM 線程之間共享的堆.堆是為所有類實例和數組分配內存的運行時數據區域.堆是在 VM 啟動時創建的.當您的線程請求單例實例時,它將指向此單例的字節碼所在的堆中的引用.它將執行適當的代碼.在您的情況下,它將為第一個請求執行第一個方法,為第二個請求執行第二個方法.它能夠這樣做是因為沒有鎖或限制阻止編譯器將程序計數器指向堆中分配此實例的區域.Singleton 類對 Java 虛擬機的唯一限制是它在該類的堆中只能有一個實例.就是這樣.除此之外,您可以從您的方法中引用它 100 次,編譯器將指向相同的字節碼并簡單地執行它.這就是為什么我們通常希望 Singleton 類是無狀態的,因為如果我們有任何線程訪問它,我們不希望內部變量因為缺乏并發控制而發生變異.

Each JVM thread has a private JVM stack, created at the same time as the thread. Now, The JVM has a heap that is shared among all JVM threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap is created on VM start-up. When your thread requests the singleton instance, it is going to point to a reference in the heap where the bytecode for this Singleton resides. It is going to execute the appropriate code. In your case, it is going to execute the first method for the first request and the second method for the second request. It's able to do this because there are no locks or restriction preventing the compiler from pointing the program counter to the area in the heap where this instance is allocated. The only restriction that the Singleton class puts on the Java Virtual Machine is that it can have only one instance in the heap of this class. That's simply it. Other than that, you can refer to it 100x times from your method, the compiler is going to point to the same bytecode and simply execute it. This is why we typically want the Singleton class to be stateless because if we any thread access it, we don't want internal variables to be mutated because of the lack of concurrency control.

如果您有任何問題,請告訴我!

Please let me know if you have any questions!

這篇關于單例 Bean 如何服務并發請求?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 亚洲精品片 | 久久精品一级 | 国产精品久久久久久久一区二区 | 五月天婷婷综合 | 亚洲欧美在线一区 | 密桃av| 免费在线观看一区二区三区 | 国产精品美女久久久久 | 免费精品视频 | 中文字幕在线观看www | 久久精品小视频 | 九九热这里只有精品在线观看 | 久久久久久国产精品免费免费男同 | 一级二级三级在线观看 | 日本激情视频中文字幕 | 国产视频欧美 | av不卡一区 | 91私密视频| 欧美视频免费 | 精品久久久久久亚洲精品 | 一区二区三区免费观看 | 亚洲精品91 | h片在线免费看 | 久久精品福利视频 | jlzzjlzz欧美大全 | 波多野吉衣久久 | 国产在线播 | 国产日韩欧美精品 | 欧美国产一区二区三区 | 欧美中文一区 | 日韩无 | 成人在线一区二区三区 | 久久久精品久久久 | 91资源在线 | 欧美精品一区二区免费 | 国产欧美一区二区精品忘忧草 | 日韩一区不卡 | 亚洲欧美中文字幕在线观看 | 国产三区av| 久久精品a| 狠狠热视频 |