3 ms·
You need to be really careful when using object pooling and carefully consider two things: synchrony and lifecycle issues. Synchrony issues occur when sharing
by cluda01 14y ago
You need to be really careful when using object pooling and carefully consider two things: synchrony and lifecycle issues.
Synchrony issues occur when sharing an object pool among multiple threads. If you're sharing an object pool and using an unsafe data structure you will run into concurrent modification exceptions. Another issue could also be that using a thread safe data structure (or more simply a syncrhonized method) will lead to huge performance degradations because it is a central point of contention. A third issue with object pools is that if you have multiple threads using an object pool it might fill up your entire heap with cached objects.
The lifecycle issues come into play when you ask the question, "Who returns the object to the pool?" For example you could have 1 thread passing an object to another thread and then returning the object to the pool. If not considered you could run into a possibility where 2 different threads are operating on an object as if they are the sole owners leading to unexpected results.