You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2002/11/02 00:58:40 UTC

cvs commit: jakarta-commons/pool/src/java/org/apache/commons/pool/impl GenericObjectPool.java

rwaldhoff    2002/11/01 15:58:40

  Modified:    pool/src/java/org/apache/commons/pool/impl
                        GenericObjectPool.java
  Log:
  extract method startEvictor
  
  Revision  Changes    Path
  1.11      +36 -36    jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
  
  Index: GenericObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- GenericObjectPool.java	31 Oct 2002 18:56:11 -0000	1.10
  +++ GenericObjectPool.java	1 Nov 2002 23:58:40 -0000	1.11
  @@ -394,12 +394,7 @@
           _testWhileIdle = testWhileIdle;
   
           _pool = new CursorableLinkedList();
  -        if(_timeBetweenEvictionRunsMillis > 0) {
  -            _evictor = new Evictor();
  -            Thread t = new Thread(_evictor);
  -            t.setDaemon(true);
  -            t.start();
  -        }
  +        startEvictor(_timeBetweenEvictionRunsMillis);
       }
   
       //--- public methods ---------------------------------------------
  @@ -596,19 +591,8 @@
        * @see #getTimeBetweenEvictionRunsMillis
        */
       public synchronized void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
  -        if(_timeBetweenEvictionRunsMillis > 0 && timeBetweenEvictionRunsMillis <= 0) {
  -            _evictor.cancel();
  -            _evictor = null;
  -            _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  -        } else if(_timeBetweenEvictionRunsMillis <= 0 && timeBetweenEvictionRunsMillis > 0) {
  -            _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  -            _evictor = new Evictor();
  -            Thread t = new Thread(_evictor);
  -            t.setDaemon(true);
  -            t.start();
  -        } else {
  -            _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  -        }
  +        _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  +        startEvictor(_timeBetweenEvictionRunsMillis);
       }
   
       /**
  @@ -838,10 +822,7 @@
               _evictionCursor.close();
               _evictionCursor = null;
           }
  -        if(null != _evictor) {
  -            _evictor.cancel();
  -            _evictor = null;
  -        }
  +        startEvictor(-1L);
       }
   
       synchronized public void setFactory(PoolableObjectFactory factory) throws IllegalStateException {
  @@ -913,8 +894,26 @@
           }
       }
       
  -    //--- package methods --------------------------------------------
  +    //--- non-public methods ----------------------------------------
   
  +    /**
  +     * Start the eviction thread or service, or when 
  +     * <i>delay</i> is non-positive, stop it
  +     * if it is already running.
  +     */
  +    protected synchronized void startEvictor(long delay) {
  +        if(null != _evictor) {
  +            _evictor.cancel();
  +            _evictor = null;
  +        }
  +        if(delay > 0) {
  +            _evictor = new Evictor(delay);
  +            Thread t = new Thread(_evictor);
  +            t.setDaemon(true);
  +            t.start();
  +        }
  +    }
  +    
       synchronized String debugInfo() {
           StringBuffer buf = new StringBuffer();
           buf.append("Active: ").append(getNumActive()).append("\n");
  @@ -962,22 +961,23 @@
        * @see #setTimeBetweenEvictionRunsMillis
        */
       class Evictor implements Runnable {
  -        protected boolean _cancelled = false;
  -
  +        private boolean _cancelled = false;
  +        private long _delay = 0L;
  +        
  +        public Evictor(long delay) {
  +            _delay = delay;
  +        }
  +        
           void cancel() {
               _cancelled = true;
           }
   
           public void run() {
               while(!_cancelled) {
  -                long sleeptime = 0L;
  -                synchronized(GenericObjectPool.this) {
  -                    sleeptime = _timeBetweenEvictionRunsMillis;
  -                }
                   try {
  -                    Thread.currentThread().sleep(sleeptime);
  +                    Thread.currentThread().sleep(_delay);
                   } catch(Exception e) {
  -                    ; // ignored
  +                    // ignored
                   }
                   try {
                       evict();
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>