Java Singleton vs static - is there a real performance benefit?

前端 未结 7 2018
抹茶落季
抹茶落季 2021-02-04 06:46

I am merging a CVS branch and one of the larger changes is the replacement wherever it occurs of a Singleton pattern with abstract classes that have a static initialisation bloc

相关标签:
7条回答
  • 2021-02-04 06:56

    If my original post was the correct understanding and the discussion from Sun that was linked to is accurate (which I think it might be), then I think you have to make a trade off between clarity and performance.

    Ask yourself these questions:

    1. Does the Singleton object make what I'm doing more clear?
    2. Do I need an object to do this task or is it more suited to static methods?
    3. Do I need the performance that I can gain by not using a Singleton?
    0 讨论(0)
  • 2021-02-04 06:59

    From a strict runtime performance point of view, the difference is really negligible. The main difference between the two lies down in the fact that the "static" lifecycle is linked to the classloader, whereas for the singleton it's a regular instance lifecycle. Usually it's better to stay away from the ClassLoader business, you avoid some tricky problems, especially when you try to reload the web application.

    0 讨论(0)
  • 2021-02-04 07:01

    Write some code to measure the performance. The answer is going to be dependent on the JVM(Sun's JDK might perform differently than JRockit) and the VM flags your application uses.

    0 讨论(0)
  • 2021-02-04 07:11

    I would use a singleton if it needed to store any state, and static classes otherwise. There's no point in instantiating something, even a single instance, unless it needs to store something.

    0 讨论(0)
  • 2021-02-04 07:11

    Static is bad for extensibility since static methods and fields cannot be extended or overridden by subclasses.

    It's also bad for unit tests. Within a unit test you cannot keep the side effects of different tests from spilling over since you cannot control the classloader. Static fields initialized in one unit test will be visible in another, or worse, running tests concurrently will yield unpredictable results.

    Singleton is generally an ok pattern when used sparingly. I prefer to use a DI framework and let that manage my instances for me (possibly within different scopes, as in Guice).

    0 讨论(0)
  • 2021-02-04 07:17

    From my experience, the only thing that matters is which one is easier to mock in unit tests. I always felt Singleton is easier and natural to mock out. If your organization lets you use JMockit, it doesn't matter since you can overcome these concerns.

    0 讨论(0)
提交回复
热议问题