Performance impact when using singleton from non-seals in Java?

Suppose that there are several instances of a class that simultaneously call service methods defined in a singleton class.

Can someone explain to me what happens at a low level when a method is called in singleton mode, when this method is already being executed by some other instance of the class? I think the JVM blocks the caller until the current caller is executed using the method. Is it correct?

Will performance improve if we move some of the methods in a singleton class to define them in a class that needs such methods?

Will performance be improved if singleton is changed to non-singleton, allowing each instance of the class that requires calling service methods in another class to create its own instance of the service class?

Suppose singleton is stateless.

Consider the situation of multiple threads calling singleton service methods.

+3
source share
4 answers

Typically, service objects are single point and stateless. Thus, there is no influence on it. if you have several objects instead of one for each class, then I think it will fill a bunch more.

+4
source

- , , . , Singleton JVM CPU .

- , , , - ?

.

, , , ?

, Singleton Singleton. , .

+3

- , , , - ? , JVM , , . ?

: JVM , , . , , .


, singleton , ?

: , - , - . . , , , . .

+3

, , , . , , , , . synchronized, , , . "Java Concurrency in Practice" Java.

As far as performance is concerned, with one single single element that can be accessed by several threads at the same time, there is no advantage (and some additional auxiliary data) for creating several objects. In general, I would first focus on clarity of the design, then measure performance and maybe try to deploy different structures if there is a problem.

+2
source

All Articles