【问题标题】:how to return the number of times Singleton accessed getInstance()?如何返回 Singleton 访问 getInstance() 的次数?
【发布时间】:2021-05-17 21:16:11
【问题描述】:

我想用一个私有的int类型accessCount()来返回通过getInstance()方法访问过Singleton的次数,但是总是报错。这是我的两段代码:

ExampleSingleton.java

public class ExampleSingleton {
  private int accessCount;
  private static ExampleSingleton instance = new ExampleSingleton();
  private ExampleSingleton() {
    System.out.println("I, the ExampleSingleton, am being created");
   }
  public static ExampleSingleton getInstance() {
    System.out.println("The sole instance of ExampleSingleton is being retrieved")
    
  }

}

ExampleTest.java

public class ExampleTest {
  public static void main(String[] args) {
    ExampleSingleton s = ExampleSingleton.getInstance();
    System.out.println("The ExampleSingleton has been "
    + "accessed via the getInstance() method "
    + s.accessCount()
    + " time(s)");
    s = ExampleSingleton.getInstance();
    System.out.println("The ExampleSingleton has been "
    + "accessed via the getInstance() method "
    + s.accessCount()
    + " time(s)");
  }
}

所以我可以得到以下输出 output

【问题讨论】:

  • 如果它真的Singleton,那么为什么要计算?
  • 你知道你永远不会增加accessCount 对吧?它也应该是静态的。
  • 我知道它是一个静态变量,如何增加访问计数?
  • getInstance 是静态的,但如果我添加 accessCount++,错误显示我从静态引用非静态。如果我将 accessCount 设为静态 int,则错误显示找不到 s.accessCount()
  • 通过添加关键字使 accessCount 变量静态化,还需要将其初始化为 0:private static int accessCount = 0;

标签: java design-patterns singleton


【解决方案1】:

此代码块应放在ExampleSingleton 类中,然后应从getInstance() 调用incrementCounter()

AtomicInteger 是一个封装整数并支持并发的类,因此它比使用标准的IntegerLong 安全得多。变量本身是private static,因此它是只读的,并且存在于单例类而不是单例实例中。

private static AtomicInteger counter = new AtomicInteger(0);


public Integer getCount() {
    return counter.get();
}
/**
 * @return the new counter
 */
private Integer incremenetCounter() {
    return counter.incrementAndGet();
}

【讨论】:

  • 哦,是的,我明白了。谢谢
【解决方案2】:

您可以使用静态AtomicInteger 类类型变量,并在重新调用实例时递增它:

public class ExampleSingleton {
    private static AtomicInteger accessCounter = new AtomicInteger(0);

    public static int getAccessCount() {
        return accessCounter.get();
    }
    private static ExampleSingleton instance = new ExampleSingleton();
    private ExampleSingleton() {
        System.out.println("I, the ExampleSingleton, am being created");
    }
    public static ExampleSingleton getInstance() {
        accessCounter.getAndAdd(1);
        System.out.println("The sole instance of ExampleSingleton is being retrieved");
        return instance;
    }
}
public class ExampleTest {
    public static void main(String[] args) {
        ExampleSingleton s = ExampleSingleton.getInstance();
        System.out.println("The ExampleSingleton has been "
                + "accessed via the getInstance() method "
                + s.getAccessCount()
                + " time(s)");
        s = ExampleSingleton.getInstance();
        System.out.println("The ExampleSingleton has been "
                + "accessed via the getInstance() method "
                + s.getAccessCount()
                + " time(s)");
    }
}

【讨论】:

  • 你的意思是我应该使用两个静态变量来计数吗?那我怎么能调用实例呢?请给我看看好吗?
  • @chuang,检查答案,不,你只需要一个 AtomicInteger 变量
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
相关资源
最近更新 更多