单例Bean如何服务并发请求? [英] How does the singleton Bean serve the concurrent request?

查看:136
本文介绍了单例Bean如何服务并发请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于单例bean如何为并发请求提供服务的问题。

I have a question regarding How the singleton bean serving the concurrent request in detail.

在堆栈Overflow中,我搜索了这个疑问, a href =http://stackoverflow.com/questions/13776728/how-does-spring-bean-handle-concurrency>链接从stackoverflow ,但我发现只有高级细节。我想要完整的细节如何单例bean服务并发请求和系统处理器将如何看到这些请求。

In stack Overflow, I have searched regarding this doubt,this is the sample link from stackoverflow, but i found only the high level details. i want full detail of how the singleton bean serve the concurrent request and How the system processor will see those request ?.

我已经在线研究了系统处理器中的并发请求处理。因为他们说处理器本身有调度器,调度器将决定哪个请求将获得进程。

I have researched regarding this concurrent request handling in the system processor in online. In that they said processor itself having the scheduler and that scheduler will decide which request will get process.

好吧。如果我有多个核心处理器,那么调度程序如何处理并发请求呢?

Ok fine. if suppose i am having the more than one core processor, that time the scheduler how does handle the concurrent request ?

任何人都可以向我解释如何singleton bean在jvm和system中为并发请求提供服务?

Can anyone explain me the step by step process on how the singleton bean serve the concurrent request in jvm and system ?

让我以实时示例解释。

Let me explain with real time example. I am having the class like sports.

class Sports{
  public void playfootball(){
  }

  public void playvolleyball(){
  }
}

两个请求即将到来,第一个请求正在执行playfootball方法,创建了类Sports的单例实例,同时另一个请求正在执行playvolleyball方法,并创建了Sports类的单例实例。

Two requests are coming and first request is executing playfootball method with created singleton instance of class Sports and at same time another request is executing playvolleyball method with 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.

首先,我对你的问题印象深刻。为了解决你的问题,需要有几个区别和理解的点。首先:Singleton模式,有时甚至称为反模式,确保JVM只有一个类的实例可用。这意味着我们基本上将全局状态引入到应用程序中。我知道你明白这一点,但它只是一个澄清点。

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. This means we're 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 create resides. Essentially what is happening is that the byte code which was generated and is representative of the singleton object you created is being executed on each one of these threads.

现在,这如何发生如下:

Now the internals of how this happens is as follows:

每个Java虚拟机线程都有一个私有Java虚拟机栈,与线程同时创建。现在,Java虚拟机具有在所有Java虚拟机线程之间共享的堆。堆是运行时数据区,从中分配所有类实例和数组的内存。堆是在虚拟机启动时创建的。当你的线程请求单例实例时,它将指向堆中的一个引用,该单元的字节码位于该引用中。它将执行相应的代码,在你的情况下,它将执行第一个请求的第一个方法和第二个请求的第二个方法。它能够这样做,因为没有锁或限制,阻止编译器将程序计数器指向堆中分配此实例的区域。 Singleton类对Java虚拟机的唯一限制是它在这个类的堆中只能有一个实例。这就是它。除此之外,你可以从你的方法引用它100倍,编译器将指向相同的字节代码,并只是执行它。这就是为什么我们通常希望Singleton类是无状态的,因为如果我们任何线程访问它,我们不希望内部变量因为缺乏并发控制而改变。

Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. Now, The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up. When you're thread requests the singleton instance, it is going to point to a reference in the heap where the byte code for this Singleton resides. It is going to execute the appropriate code, in your case it's 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 byte code 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如何服务并发请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆