Reputation: 15
Im working on a small java library and I got a Class named Operation which is a task to process, for now all the tasks are running in a singleThreadExecutor (sequentially).
I made my own Listener and everything to be able to monitor the current operation being processes.
Im storing my observers in a CopyOnWriteArrayList inside my Operation class.
My biggest concern is that i will create a lot of Operation object and i am unlikely to register more than 1-2 observers. I really dont like the idea of creating a CopyOnWriteArrayList for each Operation instance.
I thought about making my CopyOnWriteArrayList static and make a static method to let the observers register to all the operation but i dont know it seems to be a bad design idea overall...
Anyone has a better idea ?
Upvotes: 1
Views: 141
Reputation: 63134
I really dont like the idea of creating a CopyOnWriteArrayList for each Operation instance
Why not? It's the correct thing to do. If you are worried about memory or performance, then I suggest you look at the source for CopyOnWriteArrayList
- it's more efficient than you think. It's essentially backed by an array, with its Iterator
(COWIterator
) simply indexes into its backing array. new CopyOnWriteArrayList()
starts off with an empty array, so the memory overhead is the CopyOnWriteArrayList
itself.
Upvotes: 1