A
AbraKadabra
I have the following problem:
I have a pool of resources with a fixed size and a bunch of client
threads that constantly need to use an arbitrary number of resources
for a small amount of time. Once a client is done using these resources
it would return them to the pool.
Specifically, I need to implement the following interface:
/**
* Take 'n' resources.
* Once the method returns, 'n' resources are available to the caller.
* If not enough resources are available, will block (at most 'timeout'
* milliseconds) until the requested number of resources is available.
*/
public void take(int n, long timeout) throws TimeoutExpiredException;
/**
* Return 'n' resources to the pool.
* If enough resources are now available, will wake blocked threads
* that now have sufficient resources in the same order in which they
* started blocking.
*/
public void put(int n);
Note the "fairness" requirement (blocked threads should be released in
the same order they started the block). Also note that these two
methods need to be thread-safe.
The question is: Is there way to use some high-level concurrency
mechanism to solve this problem?
I actually managed to solve it using locks and condition variables, but
of course, implementation was non-trivial.
Thanks,
AbraKadabra
I have a pool of resources with a fixed size and a bunch of client
threads that constantly need to use an arbitrary number of resources
for a small amount of time. Once a client is done using these resources
it would return them to the pool.
Specifically, I need to implement the following interface:
/**
* Take 'n' resources.
* Once the method returns, 'n' resources are available to the caller.
* If not enough resources are available, will block (at most 'timeout'
* milliseconds) until the requested number of resources is available.
*/
public void take(int n, long timeout) throws TimeoutExpiredException;
/**
* Return 'n' resources to the pool.
* If enough resources are now available, will wake blocked threads
* that now have sufficient resources in the same order in which they
* started blocking.
*/
public void put(int n);
Note the "fairness" requirement (blocked threads should be released in
the same order they started the block). Also note that these two
methods need to be thread-safe.
The question is: Is there way to use some high-level concurrency
mechanism to solve this problem?
I actually managed to solve it using locks and condition variables, but
of course, implementation was non-trivial.
Thanks,
AbraKadabra