@@ -19,7 +19,7 @@ package com.github.mauricio.async.db.pool
1919import com .github .mauricio .async .db .util .{Log , Worker }
2020import java .util .concurrent .atomic .AtomicLong
2121import java .util .{TimerTask , Timer }
22- import scala .collection .mutable .ArrayBuffer
22+ import scala .collection .mutable .{ ArrayBuffer , Queue , Stack }
2323import scala .concurrent .{Promise , Future }
2424import scala .util .{Failure , Success }
2525
@@ -49,9 +49,9 @@ class SingleThreadedAsyncObjectPool[T](
4949 import SingleThreadedAsyncObjectPool .{Counter , log }
5050
5151 private val mainPool = Worker ()
52- private val poolables = new ArrayBuffer [PoolableHolder [T ]](configuration.maxObjects )
52+ private var poolables = new Stack [PoolableHolder [T ]]()
5353 private val checkouts = new ArrayBuffer [T ](configuration.maxObjects)
54- private val waitQueue = new ArrayBuffer [Promise [T ]](configuration.maxQueueSize )
54+ private val waitQueue = new Queue [Promise [T ]]()
5555 private val timer = new Timer (" async-object-pool-timer-" + Counter .incrementAndGet(), true )
5656 timer.scheduleAtFixedRate(new TimerTask {
5757 def run () {
@@ -150,10 +150,10 @@ class SingleThreadedAsyncObjectPool[T](
150150 */
151151
152152 private def addBack (item : T , promise : Promise [AsyncObjectPool [T ]]) {
153- this .poolables += new PoolableHolder [T ](item)
153+ this .poolables.push( new PoolableHolder [T ](item) )
154154
155- if (! this .waitQueue.isEmpty ) {
156- this .checkout(this .waitQueue.remove( 0 ))
155+ if (this .waitQueue.nonEmpty ) {
156+ this .checkout(this .waitQueue.dequeue( ))
157157 }
158158
159159 promise.success(this )
@@ -205,7 +205,7 @@ class SingleThreadedAsyncObjectPool[T](
205205 case e : Exception => promise.failure(e)
206206 }
207207 } else {
208- val item = this .poolables.remove( 0 ).item
208+ val item = this .poolables.pop( ).item
209209 this .checkouts += item
210210 promise.success(item)
211211 }
@@ -241,7 +241,7 @@ class SingleThreadedAsyncObjectPool[T](
241241 }
242242 }
243243 }
244- this .poolables --= removals
244+ this .poolables = this .poolables.diff( removals)
245245 }
246246
247247 private class PoolableHolder [T ](val item : T ) {
0 commit comments