public Entry borrow ( ) throws E { long now = System . currentTimeMillis ( ) ; if ( timeout > 0 ) { long accessed_ = accessed . get ( ) ; if ( now > accessed_ + Time . SECOND && accessed . compareAndSet ( accessed_ , now ) ) { Entry entry ; while ( ( entry = deque . pollLast ( ) ) != null ) { // inactiveCount.decrementAndGet(); if ( now < entry . borrowed + timeout ) { deque . offerLast ( entry ) ; // inactiveCount.incrementAndGet(); break ; } inactiveCount . decrementAndGet ( ) ; try { finalizer . accept ( entry . object ) ; } catch ( Exception e ) { // Ignored } } } } Entry entry = deque . pollFirst ( ) ; if ( entry != null ) { inactiveCount . decrementAndGet ( ) ; entry . borrowed = now ; entry . borrows ++ ; entry . valid = false ; activeCount . incrementAndGet ( ) ; return entry ; } entry = new Entry ( ) ; entry . object = initializer . get ( ) ; entry . created = entry . borrowed = now ; entry . borrows = 0 ; entry . valid = false ; activeCount . incrementAndGet ( ) ; return entry ; }
Borrow an object from the pool or create a new object if no valid objects in the pool