I am creating a simple demo system using DropWizard to create a simple REST API which will just use a HashMap
and ArrayList
for data persistence.
I am wondering about multiple requests coming in and there being race conditions to read/write the data.
Is it simple enough to synchronise access to these methods? Will that alleviate any potential issues?
This demo is for a job interview so there won't be any major load on the API. I just want to address the potential for issues and I'm wondering if synchronised methods would do the trick? Thanks.
Race conditions are solved by properly locking objects that are used by different threads/processes.
You could use Collections.synchronizedMap()
and Collections.synchronizedList()
to get properly synchronized instances, but that might not be suitable if the complete state consists of multiple objects.
In that case you should properly guard the full state by synchronizing all access to it. You would then use the unsynchronized versions of the collection instances, but synchronize access to the object that handles all state.