简体   繁体   中英

Javassist classes can be shared with multiple threads?

Can I share Javassist classes in multiple threads to gets a better performance in a web app? I think that I can create a cache with ConcurrentMap for proxyClass to avoid creation in each page request.

My code is:

ProxyFactory factory = new ProxyFactory();
factory.setFilter(IGNORE_BRIDGE_AND_OBJECT_METHODS);
factory.setInterfaces(new Class[] { type });

Class<?> proxyClass = factory.createClass(); // can I cache here after class creation?

Object proxyInstance = ...; // objenesis creates new instance here
setHandler(proxyInstance, myCustomHandlerHere);

UPDATE: I see the Javassist code, and Javassist also provides a cache.

Sure you can. Javassist compiled classes are first class classes (even if only for a short time) so it's a good idea to cache the created classes once they've been created to save yourself the overhead of recompiling them over-and-over. Plus you don't have to invent improbable names for your classes since you will only recompile the same virtual code once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM