forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePool.java
More file actions
204 lines (174 loc) · 5.6 KB
/
Copy pathSimplePool.java
File metadata and controls
204 lines (174 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* @deprecated This class is NOT a part of public API and will be dropped in 3.x versions.
*/
@Deprecated
public abstract class SimplePool<T> {
/** Initializes a new pool of objects.
* @param name name for the pool
* @param size max to hold to at any given time. if < 0 then no limit
*/
public SimplePool(String name, int size){
_name = name;
_size = size;
_sem = new Semaphore(size);
}
/** Creates a new object of this pool's type. Implementations should throw a runtime exception if unable to create.
* @return the new object.
*/
protected abstract T createNew();
/**
* override this if you need to do any cleanup
*/
public void cleanup( T t ) {
}
/**
* Pick a member of {@code _avail}. This method is called with a lock held on {@code _avail}, so it may be used safely.
*
* @param recommended the recommended member to choose.
* @param couldCreate true if there is room in the pool to create a new object
* @return >= 0 the one to use, -1 create a new one
*/
protected int pick( int recommended , boolean couldCreate ){
return recommended;
}
/**
* call done when you are done with an object form the pool
* if there is room and the object is ok will get added
* @param t Object to add
*/
public void done( T t ){
synchronized ( this ) {
if (_closed) {
cleanup(t);
return;
}
assertConditions();
if (!_out.remove(t)) {
throw new RuntimeException("trying to put something back in the pool wasn't checked out");
}
_avail.add(t);
}
_sem.release();
}
private void assertConditions() {
assert getTotal() <= getMaxSize();
}
public void remove( T t ) {
done(t);
}
/** Gets an object from the pool - will block if none are available
* @return An object from the pool
*/
public T get() throws InterruptedException {
return get(-1);
}
/** Gets an object from the pool - will block if none are available
* @param waitTime
* negative - forever
* 0 - return immediately no matter what
* positive ms to wait
* @return An object from the pool, or null if can't get one in the given waitTime
*/
public T get(long waitTime) throws InterruptedException {
if (!permitAcquired(waitTime)) {
return null;
}
synchronized (this) {
assertConditions();
int toTake = pick(_avail.size() - 1, getTotal() < getMaxSize());
T t;
if (toTake >= 0) {
t = _avail.remove(toTake);
} else {
t = createNewAndReleasePermitIfFailure();
}
_out.add(t);
return t;
}
}
private T createNewAndReleasePermitIfFailure() {
try {
T newMember = createNew();
if (newMember == null) {
throw new IllegalStateException("null pool members are not allowed");
}
return newMember;
} catch (RuntimeException e) {
_sem.release();
throw e;
} catch (Error e) {
_sem.release();
throw e;
}
}
private boolean permitAcquired(final long waitTime) throws InterruptedException {
if (waitTime > 0) {
return _sem.tryAcquire(waitTime, TimeUnit.MILLISECONDS);
} else if (waitTime < 0) {
_sem.acquire();
return true;
} else {
return _sem.tryAcquire();
}
}
/** Clears the pool of all objects. */
protected synchronized void close(){
_closed = true;
for (T t : _avail)
cleanup(t);
_avail.clear();
_out.clear();
}
public String getName() {
return _name;
}
public synchronized int getTotal(){
return _avail.size() + _out.size();
}
public synchronized int getInUse(){
return _out.size();
}
public synchronized int getAvailable(){
return _avail.size();
}
public int getMaxSize(){
return _size;
}
public synchronized String toString(){
StringBuilder buf = new StringBuilder();
buf.append("pool: ").append(_name)
.append(" maxToKeep: ").append(_size)
.append(" avail ").append(_avail.size())
.append(" out ").append(_out.size())
;
return buf.toString();
}
protected final String _name;
protected final int _size;
protected final List<T> _avail = new ArrayList<T>();
protected final Set<T> _out = new HashSet<T>();
private final Semaphore _sem;
private boolean _closed;
}