java中的单例类设计,仅用于创建10个实例

java中的单例类设计,仅用于创建10个实例,java,Java,我必须创建一个singleton类,除此之外,我必须确保 要创建的类的10个实例在创建11个实例时,它应该抛出异常,我已经提出了以下设计,请告知它是否正确。唯一的问题似乎是它将在哪里抛出异常,请告知 public final class LazySingleton { private static volatile LazySingleton instance = null; private int counter = 0; // use to track the coun

我必须创建一个singleton类,除此之外,我必须确保 要创建的类的10个实例在创建11个实例时,它应该抛出异常,我已经提出了以下设计,请告知它是否正确。唯一的问题似乎是它将在哪里抛出异常,请告知

public final class LazySingleton {
    private static volatile LazySingleton instance = null;


     private int counter = 0; // use to track the count of instance 

    // private constructor
    private LazySingleton() {
    }

    public static LazySingleton getInstance() {
        if (instance == null || counter<=10) {
            synchronized (LazySingleton.class) {
                instance = new LazySingleton();
                counter ++;
            }
        }
        return instance;
    }
}
public final class lazymingleton{
私有静态易失性LazySingleton实例=null;
private int counter=0;//用于跟踪实例的计数
//私有构造函数
私人懒汉(){
}
公共静态LazySingleton getInstance(){

if(instance==null | | counter将计数器设为
静态
变量,以便与所有其他实例共享。如果在一个实例中增加变量,其他实例也会看到它


将增量放在构造函数中,最后用if语句检查,如果
计数器
大于10,则抛出异常

使
计数器
成为一个
静态
变量,以便
LazySingleton
的所有实例共享相同的值。然后在
LazySingleton 你可以查一下

if (counter > 10) {
    //throw desired exception
}

还要确保在构造函数中增加计数器。

我认为这不再是singleton,这是一个工厂类,因为您无法访问其他最后一个对象,只能引用当前对象

买的方式,你必须设置计数器静态,也可以这样更改if语句

synchronized (LazySingleton.class) {
    if (instance == null || counter < 10) {
            instance = new LazySingleton();
            counter ++;
         }
    else{
            throw new yourCustomException();
    }
}
synchronized(LazySingleton.class){
if(实例==null | |计数器<10){
instance=new LazySingleton();
计数器++;
}
否则{
抛出新的yourCustomException();
}
}

我认为您正在寻找一个。在Java中,它可能看起来像

public final class LazySingleton {
    private static final int MAX = 10;
    private static LazySingleton[] instances = new LazySingleton[MAX];
    static {
        for (int i = 0; i < MAX; i++) {
            instances[i] = new LazySingleton(i);
        }
    }
    private int count;

    private static int counter = 0; // use to track the count of instance

    // private constructor
    private LazySingleton(int count) {
        this.count = count;
    }

    public static synchronized LazySingleton getInstance() {
        if (counter < MAX) {
            return instances[counter++];
        }
        throw new RuntimeException("Out of new instances.");
    }
}

public final class lazymingleton{
专用静态最终整数最大值=10;
私有静态LazySingleton[]实例=新LazySingleton[MAX];
静止的{
对于(int i=0;i
这个怎么样:

你想成为单身汉的班级

private static SingleTon _inst = null;
private static Map<SingleTon, Integer> map = new HashMap<SingleTon, Integer>();


private SingleTon()
{

}

public static synchronized SingleTon _instance()throws Exception
{


         if(map.size()>0)
         {
             System.out.println("Okay Object is there");
         if(!map.containsValue(10))
         {
           int temp = map.get(_inst)+1;
            map.put(_inst, temp);
            return   _inst;

         }else
         {
             throw new Exception("you reached limit");
         }
    }else
    {
        _inst = new SingleTon();
        map.put(_inst,1);
        return _inst;
    }


}

你的问题是什么?另外,我认为你需要一个数组…将
计数器
作为静态变量。这甚至不会编译,是吗?这不是线程安全的,多个线程试图同时获取实例会导致计数器上升到10以上。你的检查和设置需要在同一个同步块中是的,但是如果你有呢创建了9个实例,2个单独的线程调用getInstance?Yyes。与David指出的问题相同。同步块太小。
    for(int i=0;i<11;i++)
        {
            System.out.println("*** Calling NUmber "+i+" Object ::: "+SingleTon._instance());
        }
             *** Calling NUmber 0 Object ::: com.comparison.test.SingleTon@1befab0
               Okay Object is there
              *** Calling NUmber 1 Object ::: com.comparison.test.SingleTon@1befab0
              Okay Object is there
              *** Calling NUmber 2 Object ::: com.comparison.test.SingleTon@1befab0
               Okay Object is there
              *** Calling NUmber 3 Object ::: com.comparison.test.SingleTon@1befab0
             Okay Object is there
             *** Calling NUmber 4 Object ::: com.comparison.test.SingleTon@1befab0
            Okay Object is there
             *** Calling NUmber 5 Object ::: com.comparison.test.SingleTon@1befab0
            Okay Object is there
            *** Calling NUmber 6 Object ::: com.comparison.test.SingleTon@1befab0
            Okay Object is there
            *** Calling NUmber 7 Object ::: com.comparison.test.SingleTon@1befab0
           Okay Object is there
          *** Calling NUmber 8 Object ::: com.comparison.test.SingleTon@1befab0
         Okay Object is there
        *** Calling NUmber 9 Object ::: com.comparison.test.SingleTon@1befab0
      Okay Object is there
             Exception occuredyou reached limit
      java.lang.Exception: you reached limit
                at com.comparison.test.SingleTon._instance(SingleTon.java:31)
                at com.comparison.test.Hell.main(Hell.java:97)