@@ -19,7 +19,7 @@ package com.github.mauricio.async.db.pool
19
19
import com .github .mauricio .async .db .util .{Log , Worker }
20
20
import java .util .concurrent .atomic .AtomicLong
21
21
import java .util .{TimerTask , Timer }
22
- import scala .collection .mutable .ArrayBuffer
22
+ import scala .collection .mutable .{ ArrayBuffer , Queue , Stack }
23
23
import scala .concurrent .{Promise , Future }
24
24
import scala .util .{Failure , Success }
25
25
@@ -49,9 +49,9 @@ class SingleThreadedAsyncObjectPool[T](
49
49
import SingleThreadedAsyncObjectPool .{Counter , log }
50
50
51
51
private val mainPool = Worker ()
52
- private val poolables = new ArrayBuffer [PoolableHolder [T ]](configuration.maxObjects )
52
+ private var poolables = new Stack [PoolableHolder [T ]]()
53
53
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 ]]()
55
55
private val timer = new Timer (" async-object-pool-timer-" + Counter .incrementAndGet(), true )
56
56
timer.scheduleAtFixedRate(new TimerTask {
57
57
def run () {
@@ -150,10 +150,10 @@ class SingleThreadedAsyncObjectPool[T](
150
150
*/
151
151
152
152
private def addBack (item : T , promise : Promise [AsyncObjectPool [T ]]) {
153
- this .poolables += new PoolableHolder [T ](item)
153
+ this .poolables.push( new PoolableHolder [T ](item) )
154
154
155
- if (! this .waitQueue.isEmpty ) {
156
- this .checkout(this .waitQueue.remove( 0 ))
155
+ if (this .waitQueue.nonEmpty ) {
156
+ this .checkout(this .waitQueue.dequeue( ))
157
157
}
158
158
159
159
promise.success(this )
@@ -205,7 +205,7 @@ class SingleThreadedAsyncObjectPool[T](
205
205
case e : Exception => promise.failure(e)
206
206
}
207
207
} else {
208
- val item = this .poolables.remove( 0 ).item
208
+ val item = this .poolables.pop( ).item
209
209
this .checkouts += item
210
210
promise.success(item)
211
211
}
@@ -241,7 +241,7 @@ class SingleThreadedAsyncObjectPool[T](
241
241
}
242
242
}
243
243
}
244
- this .poolables --= removals
244
+ this .poolables = this .poolables.diff( removals)
245
245
}
246
246
247
247
private class PoolableHolder [T ](val item : T ) {
0 commit comments