真不懂?还是故意的呢?
还是回答问题吧
我觉得可以这样做
建立两个对象
ArrayList inUsedCon
ArrayList idleCon
然后实例话N个实例
存入idleCon中,每次使用则从idleCon中取出一个
将该实例放入inUsedCon中
使用完后从inUsedCon中删除,重新加入idleCon中,可以否?

解决方案 »

  1.   

    是真不懂耶,你的意思就是把这个实例化过一次的对象放到Application中去是吗?
    就是你的ArrayList inUserCon;放到Application中去,以后用时从Application中取就可以了,是不是这样的?
      

  2.   

    呵呵,当然是要新建立一个类,然后将inUserCon和idleCon放入其中
    应该差不多,我在一本说中看到一个例子,是用线程实现的,它用了两个类实现,当然它的好处就是能够同步。
      

  3.   

    例如:
    class a
    {
      ArrayList inUse
      ArrayList idle
      init()//初始化实例,放入idle中
      getCon()//取出连接
      recoverCon()//还原连接
    }
    如果你想在b类中使用连接可以如下:
    class b
    {
     a aaa=new a;
      b()
      {
        aaa.init();
      }
      methodA
     {
        Connection con aaa.getCon()
     }
    }
      

  4.   

    我的问题就是:
    class b
    {
     a aaa=new a;
    //这里我不要new a,我要用以前实例化过的连接池,就是要用old a,
    连接池里已经有一些连接,如果再实例化一个连接池,连接池就起不到它存贮连接的作用,我想的是,连接池实例化一次,然后,需要时从里面取出数据库连接,并且所有人都用这个实例化过的连接池来连接数据库.
    呵,这里或许我对连接池理解有误,还得你再指教.
      b()
      {
        aaa.init();
      }
      methodA
     {
        Connection con aaa.getCon()
     }
    }
      

  5.   

    是呀,我就是要所有的类都能够使用连接池中的连接,你是怎么实现的,我想法是放在application中,不知道能不能行.
      

  6.   

    应该是自己后台启动一个线程,我找到一段代码如下:
    import java.util.LinkedList;public class ThreadPool
    {
       static final long IDLE_TIMEOUT = 60000L;   private String name;
       private int minsize;
       private int maxsize;
       private int nextWorkerId = 0;
       private LinkedList pool = new LinkedList();   public ThreadPool() {
           this("PooledThread");
       }   public ThreadPool(String name) {
           this(name, 0, 20);
       }   public ThreadPool(String name, int minsize, int maxsize) {
           this.name = name;
           this.minsize = minsize;
           this.maxsize = maxsize;
       }   public synchronized void run(Runnable runner) {
           Worker worker;       if (runner == null) {
               throw new NullPointerException();
           }       // get a worker from free list...
           if (!pool.isEmpty()) {
               worker = (Worker) pool.removeFirst();
           } else {
               // ...no free worker available, create new one...
               worker = new Worker(name + "-" + ++nextWorkerId);
               worker.start();
           }       // ...and wake up worker to service incoming runner
           worker.wakeup(runner);
       }   // Notified when a worker has idled timeout
       // @return true if worker should die, false otherwise
       synchronized boolean notifyTimeout(Worker worker) {
           if (worker.runner != null) {
               return false;
           }
           if (pool.size() > minsize) {
               // Remove from free list
               pool.remove(worker);
               return true; // die
           }
           return false; // continue
       }   // Notified when a worker has finished his work and
       // free to service next runner
       // @return true if worker should die, false otherwise
       synchronized boolean notifyFree(Worker worker) {
           if (pool.size() < maxsize) {
               // Add to free list
               pool.addLast(worker);
               return false; // continue
           }
           return true; // die
       }   // The inner class that implement worker thread
       class Worker extends Thread {
           Runnable runner = null;       public Worker(String name) {
               super(name);
           }       synchronized void wakeup(Runnable runner) {
               this.runner = runner;
               notify();
           }       public void run() {
               for (;;) {
                   synchronized (this) {
                       if (runner == null) {
                           try {
                               wait(IDLE_TIMEOUT);
                           } catch (InterruptedException e) {}
                       }
                   }               // idle timed out, die or put into free list
                   if (runner == null) {
                        if (notifyTimeout(this))
                            return;
                        else
                            continue;
                   }               try {
                       runner.run();
                   } finally {
                       runner = null;
                       if (notifyFree(this))
                           return;
                   }
               }
           }
       }
    }
      

  7.   

    呵呵,好的,我把连接池放在application里看看会怎么样!