meszibalu commented on a change in pull request #2685:
URL: https://github.com/apache/hbase/pull/2685#discussion_r528303073



##########
File path: hbase-client/src/main/java/org/apache/hadoop/hbase/util/PoolMap.java
##########
@@ -321,62 +297,45 @@ public R get() {
    * @param <R>
    *          the type of the resource
    */
-  static class ThreadLocalPool<R> extends ThreadLocal<R> implements Pool<R> {
-    private static final Map<ThreadLocalPool<?>, AtomicInteger> poolSizes = 
new HashMap<>();
+  static class ThreadLocalPool<R> implements Pool<R> {
+    private final Map<Thread, R> resources;
 
     public ThreadLocalPool() {
+      resources = new HashMap<>();
     }
 
     @Override
-    public R put(R resource) {
-      R previousResource = get();
-      if (previousResource == null) {
-        AtomicInteger poolSize = poolSizes.get(this);
-        if (poolSize == null) {
-          poolSizes.put(this, poolSize = new AtomicInteger(0));
-        }
-        poolSize.incrementAndGet();
-      }
-      this.set(resource);
-      return previousResource;
-    }
+    public R getOrCreate(PoolResourceSupplier<R> supplier) throws IOException {
+      Thread myself = Thread.currentThread();
+      R resource = resources.get(myself);
 
-    @Override
-    public void remove() {
-      super.remove();
-      AtomicInteger poolSize = poolSizes.get(this);
-      if (poolSize != null) {
-        poolSize.decrementAndGet();
+      if (resource == null) {
+        resource = createResource(supplier);
+        resources.put(myself, resource);
       }
+
+      return resource;
     }
 
     @Override
-    public int size() {
-      AtomicInteger poolSize = poolSizes.get(this);
-      return poolSize != null ? poolSize.get() : 0;
+    public boolean remove(R resource) {
+      Thread myself = Thread.currentThread();
+      return resources.remove(myself, resource);
     }

Review comment:
       It's a very good observation, thanks! I changed it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to