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

單例 Bean 如何服務并發(fā)請求?

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

問題描述

限時送ChatGPT賬號..

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

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

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

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.

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

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.

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

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

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

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 類的已創(chuàng)建單例實例上執(zhí)行 playFootball 方法.同時,另一個請求正在對 Sports 類的同一個已創(chuàng)建單例實例執(zhí)行 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.

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

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.

現(xiàn)在是內部結構.

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

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.

現(xiàn)在內部情況如下:

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

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 如何服務并發(fā)請求?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

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:獲取當前星期幾的值)
主站蜘蛛池模板: 成人在线中文字幕 | 九九热在线精品视频 | 国产成人久久精品一区二区三区 | 韩日视频在线观看 | 91免费高清视频 | 国产精品视频一二三区 | 国产一区欧美 | 精品欧美一区二区精品久久久 | 色吧色综合 | 久久一区二区精品 | 亚洲天堂中文字幕 | 日韩av在线中文字幕 | 国产视频久久久 | 国产高清久久 | 国内精品久久久久久 | 天堂av影院 | 91高清在线观看 | 欧美一级欧美三级在线观看 | 中文字幕第一页在线 | 最新免费视频 | 亚洲性在线 | 国产高清在线观看 | 国产精品亚洲视频 | 九九热在线观看视频 | 国产精品特级毛片一区二区三区 | 日韩国产欧美视频 | wwww.xxxx免费 | h视频在线免费 | 国产99久久| 亚洲久久 | 国产精品毛片一区二区在线看 | 亚洲视频一区二区三区四区 | 亚洲品质自拍视频网站 | 国产精品久久久久久av公交车 | 精品国产91 | 99综合| 成人片网址 | 午夜视频在线观看视频 | 永久看片 | 在线高清免费观看视频 | 久久久免费电影 |