Mercurial > stress-tester
changeset 745:6c06ad27997a
cleanup
author | Devel 1 |
---|---|
date | Thu, 07 Dec 2017 14:34:38 +0100 |
parents | 50e2a0229e7b |
children | 05758e673530 |
files | stress-tester/src/main/java/com/passus/st/data/ObjectPool.java stress-tester/src/main/java/com/passus/st/data/ObjectPoolImpl.java |
diffstat | 2 files changed, 0 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/data/ObjectPool.java Thu Dec 07 10:31:35 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package com.passus.st.data; - -import java.io.Closeable; - -/** - * - * @author mikolaj.podbielski - */ -public interface ObjectPool<T> extends Closeable { - - T get(); - - void release(T t); - - - public interface Factory<T> { - - T create(); - } -}
--- a/stress-tester/src/main/java/com/passus/st/data/ObjectPoolImpl.java Thu Dec 07 10:31:35 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -package com.passus.st.data; - -import java.io.IOException; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingDeque; - -/** - * - * @author mikolaj.podbielski - */ -public class ObjectPoolImpl<T> implements ObjectPool<T> { - - private final ObjectPool.Factory<T> factory; - private final BlockingQueue<T> objects; - private final int capacity; - private int borrowed = 0; - - public ObjectPoolImpl(Factory<T> factory, int initialSize, int capacity) { - this.factory = factory; - this.capacity = capacity; - objects = new LinkedBlockingDeque<>(capacity); - for (int i = 0; i < initialSize; i++) { - objects.add(factory.create()); - } - } - - @Override - public T get() { - synchronized (objects) { - if ((!objects.isEmpty())) { - ++borrowed; - return objects.poll(); - } else if (borrowed < capacity) { - ++borrowed; - return factory.create(); - } else { - return null; - } - } - } - - @Override - public void release(T t) { - synchronized (objects) { - --borrowed; - objects.offer(t); - } - } - - @Override - public void close() throws IOException { - } - -}