1

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 ?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.