Skip to content

Commit

Permalink
rewrote recursion with a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzardo committed Jan 22, 2024
1 parent 370e787 commit e98f724
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/main/java/com/wizzardo/http/ReadableByteArrayPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface WithHolder<T> {

private static class SoftHolder<T extends WithHolder<T>> implements Holder<T> {
final Pool<T> pool;
volatile SoftReference<T> ref;
final SoftReference<T> ref;

private SoftHolder(Pool<T> pool, T value) {
this.pool = pool;
Expand All @@ -35,13 +35,16 @@ private SoftHolder(Pool<T> pool, T value) {
public T get() {
T value = ref.get();
if (value == null) {
if (pool.size() > 0)
return pool.holder().get();
else {
T t = pool.create();
new SoftHolder<>(pool, t);
return t;
while (pool.size() > 0) {
SoftHolder<T> holder = (SoftHolder<T>) pool.holder();
T t = holder.ref.get();
if (t != null)
return pool.reset(t);
}

T t = pool.create();
new SoftHolder<>(pool, t);
return t;
} else {
pool.reset(value);
}
Expand All @@ -50,7 +53,8 @@ public T get() {

@Override
public void close() {
pool.release(this);
if (ref.get() != null)
pool.release(this);
}
}

Expand Down

0 comments on commit e98f724

Please sign in to comment.