想想很容易,写以来就难了,主要是多线程调用的问题。
对象类型已定,就不用泛型了

解决方案 »

  1.   


    public interface IPoolable extends IObject
    {
    IPoolX getPool();

    void lock();
    boolean isLocked();
    void unlock();

    String getPoolContext();
    }
    public interface IPoolX
    {
    public ObjectInitialContext getObjectContext();

    public IPoolable  getInstance();
    public IPoolable  newInstance(boolean freeObject);

    public void resetObjectContext();

    public void  freeInstance(IPoolable obj);

    public String getXElement();

    public int getPoolSize();

    public boolean allowNewWhenOutOfSize(); //当超出限度时能都创建新对象?
    } protected IPoolable getInstance(String groupID)
    {
    MyObjectList thisGroup = (MyObjectList)m_listGroup.get(groupID);

    while(true)
    {
    for(int i=1,n=thisGroup.size();i<=n;i++)
    {
    IPoolable IPO = (IPoolable)thisGroup.get(i);

    if(!IPO.isLocked())
    {
    IPO.lock();

    MyDebug.dumpConsole(
    "<"+this.getXElement()+">"
    +"("+i+")"
    +this.getIDII()
    +"{"+groupID+"}@"
    +IPO.getIDII()
    +IPO.getPoolContext()
    +"</"+this.getXElement()+">");

    return IPO;
    }
    }

    MyDebug.DEBUGERROR(this,"getInstance",getIDII()+"@"+groupID+" POOL USED UP");

    if(allowNewWhenOutOfSize())
    return newInstance(true);
    }
    }
      

  2.   

    这应该不是java代码把。想求具体实现,多线程调用肯定会有问题。
    有质量高的代码参考?