简体   繁体   English

单例类(也是单例)中是否有静态类?

[英]Is a static class within a singleton class, also singleton?

I'm trying to learn whether in Java 8, a nested static class in a controller (singleton class) is also static and could be shared between requests? 我试图了解在Java 8中,控制器中的嵌套静态类(单例类)是否也是静态的,并且可以在请求之间共享?

This is legacy code I'm cleaning up because of a possible race condition: The controller had multiple private fields. 这是我正在清理的旧代码,因为可能存在争用条件:控制器具有多个私有字段。 I moved them to a static nested class and created an instance of that class each time the request hits the controller. 每当请求到达控制器时,我便将它们移到静态嵌套类并创建该类的实例。 Then I pass that object around to private methods for calculations. 然后,我将该对象传递给私有方法进行计算。

I'm being told that static nested classes in singletons have only one instance of the sub-class in memory, and if it's hit with 2 requests, the second one will stick. 我被告知,静态内部类的单身人士在内存中的子类只有一个实例,如果它与2个请求命中,第二个会坚持下去。 Also being warned that someone could move this static class outside, which is not a good approach to take (?) 还被警告说有人可以将这个静态类移到外面,这不是采取(?)的好方法

There are a lot of answers around the difference between static class & singletons. 关于静态类和单例之间的区别,有很多答案。 Have found on Oracle docs: In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. 已经在Oracle文档中发现:实际上,静态嵌套类是行为上已经被嵌套在另一个顶级类包装方便的顶级类。

=== But I haven't found anything about a static nested class IN a singleton === ===但是我还没有发现有关静态嵌套类的任何信息===

I tried it out: paused a thread in handleRequest and started a second one, and found the instances of the static nested class were different and contained different values. 我尝试了一下:暂停在的handleRequest一个线程,并开始了第二个,发现静态嵌套类的实例是不同的,含有不同的值。 This is what I expect, given the documentation, but I don't know for sure, because I can't find anything about a static nested class WITHIN A SINGLETON. 这是我所期望的,给出的文档,但我不知道是肯定的,因为我无法找到一个静态嵌套类中一个单身任何东西。

Am I missing something? 我想念什么吗? Is it possible this will fail? 这有可能会失败吗? Is there a better solution? 有更好的解决方案吗?

public class MyController extends WebContentGenerator implements Controller {

  @Override
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
  {
    ReportParameters params = new ReportParameters();
    initVars(request, params);
    doWork(params);
    return null;
  }

  private void initVars(HttpServletRequest request, ReportParameters params)
  {
    params.flag = "Y".equals(request.getParameter("flag"));
    params.message = "Hello world";
  }

  private void doWork(ReportParameters params)
  {
    if (params.flag)
      params.message = "foo";
  }

  private static class ReportParameters
  {
    private boolean flag;
    private String message;
  }
}

A static nested class is no different than a top-level class: every time you use new to create an instance, you... create an instance. 静态嵌套类与顶级类没有什么不同:每次使用new来创建实例时,都需要创建一个实例。 If you create an instance more than once, then by definition, it's not a singleton. 如果创建一个实例不止一次,那么根据定义,它不是单例。 The fact that it's created from a singleton is competely irrelevant. 从单例创建的事实在竞争上是无关紧要的。 The JVM doesn't even have the notion of a singleton: it's just an architectural choice. JVM甚至没有单例的概念:这只是一种体系结构选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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