jhalterman / expiringmap

A high performance thread-safe map that expires entries
Apache License 2.0
1.01k stars 142 forks source link

Thread Pool configuration #38

Open siva38 opened 8 years ago

siva38 commented 8 years ago

Can the thread pool configuration be exposed for Expirer and Listener thread pools?

If a single threaded expirer may not be sufficient and an unbounded thread pool an over kill for certain cases, can the caller be asked to optionally supply thread pool executor instance?

jhalterman commented 8 years ago

Sure this could be configurable. I'm curious if you've actually hit a situation where the expiration thread, in particular, is unable to keep up with expirations. Has that happened? Any details?

siva38 commented 8 years ago

I am planning to use this in an application where the TPS no's would be huge. I feel the single threaded expirer thread fits the scenario, but i saw a new thread created for clearing expired entries.

I tried to add 50 k entries with expiration of 3000 ms. After 5000 ms, i saw close to 23k entries cleared of cache.So i thought having an option to configure more threads for expiration would speed up the clean up process.

Also a bit hesitant to use the unbounded thread pool for listener. If these could be configured via builder it would be great

jhalterman commented 8 years ago

Thanks for the use case. I don't think it will be quite as straightforward as throwing more threads at it since there will be some lock contention when attempting to remove entries from the internal map.

siva38 commented 8 years ago

Thanks. Can the listener thread be made configurable to prevent unbounded thread creation

jhalterman commented 8 years ago

Another option is we could use a fixed thread pool instead of cached. That opens up the potential for new problems... but what do you think?

CodingFabian commented 7 years ago

Here is a related use case: under very restricted resources, I might want to use inline expiration (on access/iteration) instead of having a thread work the expiration.

CodingFabian commented 7 years ago

and another "problem" if you use the ExpiringMap in an environment where the class could get unloaded, there is no "API-way" to stop the expirer and listenerService. thus leaking those services and threads

lapo-luchini commented 6 years ago

Yes, it generates:

org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [test] appears to have started a thread named [ExpiringMap-Expirer] but has failed to stop it. This is very likely to create a memory leak.

Being able to call EXPIRER.awaitTermination(…) would be nice.

lapo-luchini commented 5 years ago

I created PR #62 for that last part.

jhalterman commented 5 years ago

Interesting alternative... I left some comments on the PR. Interested to get feedback.