diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-07-01 12:57:31 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-07-01 12:57:31 +0200 |
commit | d79a53fe0d9d2f2d4b399e44e13495cc837614e5 (patch) | |
tree | 2b8a6eda2b1f3cfee47d0564c962642dbaafa2f3 /yolean/src | |
parent | d78f33d83656b483f8402646135f78caceea3601 (diff) |
Use closed boolean and cleanup
Diffstat (limited to 'yolean/src')
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java | 24 | ||||
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java | 9 |
2 files changed, 29 insertions, 4 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java index 24d4cfe4318..a00de4866d0 100644 --- a/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java +++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java @@ -4,24 +4,41 @@ package com.yahoo.yolean.concurrent; import java.util.Iterator; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.Supplier; /** + * A pool of a resource. This create new instances of the resource on request until enough are created + * to deliver a unique one to all threads needing one concurrently and then reuse those instances + * in subsequent requests. + * * @author baldersheim */ public class ConcurrentResourcePool<T> implements Iterable<T> { private final Queue<T> pool = new ConcurrentLinkedQueue<>(); - private final ResourceFactory<T> factory; + private final Supplier<T> factory; + // TODO: Deprecate public ConcurrentResourcePool(ResourceFactory<T> factory) { + this.factory = factory.asSupplier(); + } + + public ConcurrentResourcePool(Supplier<T> factory) { this.factory = factory; } + /** + * Allocates an instance of the resource to the requestor. + * The resource will be allocated exclusively to the requestor until it calls free(instance). + * + * @return a reused or newly created instance of the resource + */ public final T alloc() { - final T e = pool.poll(); - return e != null ? e : factory.create(); + T e = pool.poll(); + return e != null ? e : factory.get(); } + /** Frees an instance previously acquired bty alloc */ public final void free(T e) { pool.offer(e); } @@ -30,4 +47,5 @@ public class ConcurrentResourcePool<T> implements Iterable<T> { public Iterator<T> iterator() { return pool.iterator(); } + } diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java index f926283a47f..3a99b189ed8 100644 --- a/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java +++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java @@ -1,11 +1,18 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.yolean.concurrent; +import java.util.function.Supplier; + /** * @author baldersheim - * @since 5.2 */ +// TODO: Deprecate public abstract class ResourceFactory<T> { public abstract T create(); + + public final Supplier<T> asSupplier() { + return () -> create(); + } + } |