23
23
import java .util .concurrent .Semaphore ;
24
24
import java .util .concurrent .ThreadLocalRandom ;
25
25
import java .util .concurrent .TimeUnit ;
26
+ import java .util .concurrent .atomic .AtomicInteger ;
26
27
27
28
import static com .google .common .base .Preconditions .checkState ;
28
29
@@ -36,11 +37,11 @@ public class ObjectPoolPartition<T>
36
37
private final PoolConfig config ;
37
38
private final BlockingQueue <Poolable <T >> objectQueue ;
38
39
private final ObjectFactory <T > objectFactory ;
39
- private int totalCount ;
40
40
private final String host ;
41
41
private final int socketTimeout ;
42
42
private final int connectTimeout ;
43
43
private final Semaphore takeSemaphore ;
44
+ private final AtomicInteger totalCount ;
44
45
45
46
public ObjectPoolPartition (ObjectPool <T > pool , PoolConfig config ,
46
47
ObjectFactory <T > objectFactory , BlockingQueue <Poolable <T >> queue , String host , String name )
@@ -52,14 +53,14 @@ public ObjectPoolPartition(ObjectPool<T> pool, PoolConfig config,
52
53
this .host = host ;
53
54
this .socketTimeout = config .getSocketTimeoutMilliseconds ();
54
55
this .connectTimeout = config .getConnectTimeoutMilliseconds ();
55
- this .totalCount = 0 ;
56
+ this .totalCount = new AtomicInteger () ;
56
57
this .log = new CustomLogger (name , host );
57
58
this .takeSemaphore = new Semaphore (config .getMaxSize (), true );
58
59
try {
59
60
for (int i = 0 ; i < config .getMinSize (); i ++) {
60
61
T object = objectFactory .create (host , socketTimeout , connectTimeout );
61
62
objectQueue .add (new Poolable <>(object , pool , host ));
62
- totalCount ++ ;
63
+ totalCount . incrementAndGet () ;
63
64
}
64
65
}
65
66
catch (Exception e ) {
@@ -116,11 +117,11 @@ private Poolable<T> tryGetObject() throws Exception
116
117
try {
117
118
T object = objectFactory .create (host , socketTimeout , connectTimeout );
118
119
poolable = new Poolable <>(object , pool , host );
119
- totalCount ++ ;
120
- log .debug (String .format ("Added a connection, new totalCount: %d , queueSize: %d" , totalCount , objectQueue .size ()));
120
+ totalCount . incrementAndGet () ;
121
+ log .debug (String .format ("Added a connection, Pool state: totalCount: %s , queueSize: %d" , totalCount , objectQueue .size ()));
121
122
}
122
123
catch (Exception e ) {
123
- log .warn (String .format ("Unable create a connection. Pool state: totalCount=%d queueSize=%d" , totalCount , objectQueue .size ()), e );
124
+ log .warn (String .format ("Unable create a connection. Pool state: totalCount=%s queueSize=%d" , totalCount , objectQueue .size ()), e );
124
125
if (poolable != null ) {
125
126
objectFactory .destroy (poolable .getObject ());
126
127
poolable .destroy ();
@@ -144,20 +145,20 @@ public boolean decreaseObject(Poolable<T> obj)
144
145
return true ;
145
146
}
146
147
147
- private synchronized void objectRemoved ()
148
+ private void objectRemoved ()
148
149
{
149
- totalCount -- ;
150
+ totalCount . decrementAndGet () ;
150
151
}
151
152
152
- public synchronized int getTotalCount ()
153
+ public int getTotalCount ()
153
154
{
154
- return totalCount ;
155
+ return totalCount . get () ;
155
156
}
156
157
157
158
// set the scavenge interval carefully
158
159
public void scavenge () throws InterruptedException
159
160
{
160
- int delta = this .totalCount - config .getMinSize ();
161
+ int delta = this .totalCount . get () - config .getMinSize ();
161
162
if (delta <= 0 ) {
162
163
log .debug ("Scavenge for delta <= 0, Skipping !!!" );
163
164
return ;
@@ -195,7 +196,7 @@ public void scavenge() throws InterruptedException
195
196
public synchronized int shutdown ()
196
197
{
197
198
int removed = 0 ;
198
- while (this .totalCount > 0 ) {
199
+ while (this .totalCount . get () > 0 ) {
199
200
Poolable <T > obj = objectQueue .poll ();
200
201
if (obj != null ) {
201
202
decreaseObject (obj );
0 commit comments