io.baltoro.client.WorkerPool Maven / Gradle / Ivy
package io.baltoro.client;
import java.util.concurrent.ConcurrentLinkedQueue;
public class WorkerPool
{
private static ConcurrentLinkedQueue freeReq = new ConcurrentLinkedQueue<>();
private static ConcurrentLinkedQueue busyReq = new ConcurrentLinkedQueue<>();
//private static Set busyReq = new HashSet<>(50);
private static ConcurrentLinkedQueue freeRes = new ConcurrentLinkedQueue<>();
private static ConcurrentLinkedQueue busyRes = new ConcurrentLinkedQueue<>();
//private static Set busyRes = new HashSet<>(50);
static RequestWorker getRequestWorker()
{
if(freeReq.size() == 0)
{
RequestWorker worker = new RequestWorker();
worker.setName("RequestWorker-"+worker.getName());
worker.start();
busyReq.add(worker);
return worker;
}
RequestWorker worker = freeReq.poll();
busyReq.add(worker);
return worker;
}
static ResponseWorker getResponseWorker()
{
if(freeRes.size() == 0)
{
return null;
}
ResponseWorker worker = freeRes.poll();
busyRes.add(worker);
return worker;
}
static void done(RequestWorker worker)
{
worker.clear();
busyReq.remove(worker);
freeReq.add(worker);
}
static void done(ResponseWorker worker)
{
worker.clear();
busyRes.remove(worker);
freeRes.add(worker);
}
static String info()
{
return "freeReq("+freeReq.size()+"), busyReq:("+busyReq.toArray().length+"), freeRes("+freeRes.size()+"), busyRes("+busyRes.size()+")";
}
}