Tudor, .
However, if you have a requirement that only x orders (x = 5000 for your case) can be processed at any given time, you can use the class java.util.concurrent.Semaphore. This is done specifically for use cases where you can only have a fixed number of tasks to perform - this is called permissions in terminologySemaphore
If you perform immediate processing, you can proceed with
private Semaphore semaphore = new Semaphore(5000);
public void process(Order order)
{
if (semaphore.tryAcquire())
{
try
{
}
finally
{
semaphore.release();
}
}
else
{
throw new IllegalStateException("can't take more orders");
}
}
If this takes more than this (requires human input, starting another thread / process, etc.), you need to add a callback to complete the processing, for example:
private Semaphore semaphore = new Semaphore(5000);
public void process(Order order)
{
if (semaphore.tryAcquire())
{
}
else
{
throw new IllegalStateException("can't take more orders");
}
}
public void processingFinished(Order order)
{
semaphore.release();
}
jmruc source
share