简体   繁体   中英

Singleton class or instance in java

I understand that in Spring framework, the default scope of the bean is Singleton. It means that only one instance of the bean is created per the container.

But I am not able to understand practically below scenario.

For example class , let say class "SingleTonClass" is configured as singleton bean in spring context.xml, 1) for every time there is a request for this class ,does it use same instance. If yes then if I make a call like new SingleTonClass().sum(3,4) from another class and new SingleTonClass().sum(5,6) , will give me the results correctly or there will be a problem some times?

2) if same instance is used by all the requests, how is it possible behind the scenes that using only one instance is used by all the requests? if there are 100 requests to the same singleton class , each request has to wait until previous request is finished?

Edit:

Let say I am using @Autowired and not new operator

In my main class, I am trying to use bean configured using spring. Let say if MyMain is called multiple times(may or may not be concurrent calls), I want to understand point 2 in my post above(edit)

public class MyMain() {

@Autowired
SingleTonClass singleTonClass;

}




   public class SingleTonClass {

    private int a=0;
    private int b=0;

    public int sum(int a ,int b) {

    return a+b;
    }
  }

When you do a new SingleTonClass() you will always get a new copy of the class. To use the Spring instancing feature you need to let Spring do your configuration for you for example by using the @Autowired annotation.

As far as the issue of multiple requests hitting the single instance at the same time, you must either make your class stateless or thread-safe. This is a topic that can't be answered simply, but you can start here.

You need to write stateless beans (means there should not be any data storage with in bean) in order to not to worry about thread safety. Or you need to handle thread safety by yourself. Spring by default create singleton non-thread safe instances of a class. Even "prototype" scope is not thread safe. When you define scope as prototype, it just means spring will create a new instance each time it is injected in another component, NOT that an instance be created each time these component executed.

For example, class A injected in two components as bean a1 and a2. There will only two instances in the SpringContext, even when millions requests are served by these components.

See this question to learn more about different design strategies you can follow.

By the way, I do not see any need of private int a, b declaration at class level in your example. In fact it is not even used and hence this bean is thread safe.

1) If you rely on Spring to manage your beans and as you say use the default singleton scope then the same instance will be used by the other beans. However if you instantiate the class yourself (for example by using new ) then there will be a new instance of your class each time.

2) If you have 100 concurrent requests, then there will be 100 threads and all of them can call your class simultaneously without blocking. That said, you can cause the threads to wait for each other (for example by using synchronized or any Lock class). Naturally, performance will suffer since you now have a situation where 99 threads are waiting. The only reason why you would prefer such a solution is if your bean has a shared, mutable, state, and that is something I advise against. Along these lines you can remove the instance variables a and b from your example. After all, they were not used anyway since both a and b were passed as method arguments:

public class SingleTonClass {

    public int sum(int a ,int b) {
        return a+b;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM