Let say there is an array of strings, I have to process. I'm using a «heavy» third-part object which gets the string and performs its analysis. In order to optimize a performance, I create a number of threads (according to a number of CPU cores), separate the array to the number of sub-arrays and each thread executes the logic described above with his portion of strings.
I have two options:
- To run the threads and inside of each thread initialize this «heavy» third-part object and pass strings. This means I initialize «heavy» object according to a number of threads.
- To initialize one time this «heavy» object and pass the reference to this object to each thread.
The approach #2 seems to me to be more correct, thus, it saves extra «heavy» object initialization.
My question:
Are there any pitfalls or design lacks with the implementation of approach #2?