aboutsummaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2021-07-01 12:57:31 +0200
committerJon Bratseth <bratseth@gmail.com>2021-07-01 12:57:31 +0200
commitd79a53fe0d9d2f2d4b399e44e13495cc837614e5 (patch)
tree2b8a6eda2b1f3cfee47d0564c962642dbaafa2f3 /yolean
parentd78f33d83656b483f8402646135f78caceea3601 (diff)
Use closed boolean and cleanup
Diffstat (limited to 'yolean')
-rw-r--r--yolean/abi-spec.json4
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java24
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java9
3 files changed, 32 insertions, 5 deletions
diff --git a/yolean/abi-spec.json b/yolean/abi-spec.json
index 82bf59ebf87..85aaaf5f64e 100644
--- a/yolean/abi-spec.json
+++ b/yolean/abi-spec.json
@@ -169,6 +169,7 @@
],
"methods": [
"public void <init>(com.yahoo.yolean.concurrent.ResourceFactory)",
+ "public void <init>(java.util.function.Supplier)",
"public final java.lang.Object alloc()",
"public final void free(java.lang.Object)",
"public java.util.Iterator iterator()"
@@ -209,7 +210,8 @@
],
"methods": [
"public void <init>()",
- "public abstract java.lang.Object create()"
+ "public abstract java.lang.Object create()",
+ "public final java.util.function.Supplier asSupplier()"
],
"fields": []
},
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();
+ }
+
}