0

For a stock market algorithm, I am implementing an observable pattern for handling quotes. The "observers" are a list of triggers. The issue, is that this list can change (add a trigger, remove a trigger, etc), so I have to deal with concurrent modification errors.

I for sure do not want to use synchronize on the list or a copyOnWriteArrayList, as that is way too slow. The handling of quotes needs to be async. I only want to lock the list if the list changes. I'm thinking about using a ReentrantReadWriteLock. When the list needs to change, acquire the write lock. If handling a quote, use a read lock. Is this the best approach to dealing with this? My goal is completely async handling of quotes, but to lock the list if I need to modify it

Ethan Solomon
  • 183
  • 1
  • 4
  • 12
  • 2
    (a) are you positive this would be too slow? (b) a CopyOnWriteArrayList is not sychnorized and does not require synchronization. It would probably fit your use case nicely. – assylias Jul 05 '21 at 14:03
  • for a, am i positive what would be slow? synchronize? It would break async handling? for b, true, but copy on write array list makes a deep copy every iteration I believe. that is unnecessary, and I believe a read lock is faster. open to suggestions obviously – Ethan Solomon Jul 05 '21 at 14:27
  • 1
    COWAList doesn't copy on each iteration, only on mutation - so unless you keep adding and removing observers, that should not happen too often. – assylias Jul 05 '21 at 15:02

0 Answers0