Search code examples
javamultithreadingtomcatservletspool

Pool of threads tied to resources on the server


I have a Java servlet that operates with a heavy-weight and thread-unsafe resource to handle user requests. The resource is an object that needs a long time to be instantiated (up to 10 seconds) and takes a large amount of memory. But when the object is allocated, it takes a short time to run its method I need to process a request.

There can be several such resources, different from each other. Each request comes with an ID, which points out on the certain resource. I wish to implement a pool of such resources, so that requests with the same IDs will not instantiate a new object, but will pick one from the pool.

The scheme is following:

  • after the request has been received, servlet checks whether a resource with the requested ID is in the pool
  • if not, servlet creates one and provides it
  • if the resource is already instantiated, the request goes into a queue to be executed, doPost waits for it.

The operation over different resources must be concurrent, but synchronized within the same resource.

I am new to multithreading in Java, and the ThreadPoolExecutor does not seem to be usable as is, so I would be appreciated for an advice how to implement the above described scheme. Thanks.


Solution

  • You are correct - ThreadPoolExecutor is not what you want. It is simply a pool of threads to run tasks with, not a shared resource collection.

    What you want is a cache. It needs to create a resource and return it to requesting threads to use, and reuse the things it returned previously. Also, the resource returned must be thread-safe (So if your underlying resources are not, you may need to write synchronized wrappers for them).

    There are a number of thread-safe caches around, quite a few of them - opensource. Try those out, it shouldn't be too difficult to configure them for your use case (it seems fairly typical).

    It is possible and not too difficult to implement a make-shift cache of your own, but you're far better off using a third-party solution if you are new to multithreading.