I’m using a thrid-party lib that has a static synchronized method, i want to execute this method in parallel without modifiying its source.
What can i do ?
Should i create an executor service with a thread factory that sets a new context class loader ?
Thank you
You could certainly in theory load the library twice in separate class loaders. Practice might be more difficult.
In order to link to link to the copy in different class loaders you’ll need to also load your code multiple times in those class loaders (or a child class loader). Common communication between the code instances should go through code loaded into a common base class. Keep reflection to an absolute minimum. Classes loaded by different class loaders will be incompatible even though they have the same name, which usually manifests as bizarre
ClassCastExceptions. For instance, the runtime object and cast type have the same name. Following? Are you sure you want to do this?Probably there is a reason why the method is synchronised. Probably the reason is really bad. There will be mutable statics in there. As you’ve loaded the code more than once there will be multiple copies of these mutable statics. If they are just caches that might not be too bad. However, the library almost certainly really sucks.
NB: The thread context class loader has very little to do with anything except screwing things up.