If the authors of the library you want to use have not developed your code to be thread safe, then you can do little but prevent your two threads from accessing them simultaneously.
You can pull some tricks with class loaders, but this usually leads to a whole new world of complexity. To elaorate, you can load the same class two times (or more) in one JVM if you use different class loaders - therefore, you can effectively obtain independent copies of static variables. This use of separate class loaders is used by some Java EE application servers. This, in turn, leads to the fact that classloader and classpath will be used when the libraries themselves begin to perform some reflection and dynamic loading of classes. I would not recommend this assessment if your need is not very great.
According to your preferences:
1). You have a single threaded worker for unsafe code. Try to do as much work as possible in your application with mulit-threaded by going into Worker as little as possible.
2). If the employee is the main part of your processing, and therefore you really need to perform parallel execution, pull the employee into several separate processes, use some IPC communication for sharing. It looks like a JMS outsourcing solution might work well.
3). If you cannot afford to outwit IPC, try finding an alternative to streams, an alternative to streaming, or if you have influence on authors, ask them to fix the code. It really should not be so difficult to increase their parallelism.
source share