All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.baltoro.client.WorkerPool Maven / Gradle / Ivy

There is a newer version: 4.0.12
Show newest version
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()+")";
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy