There are several streams that access the same hashmap (not streaming).
It looks like you are using this HashMap in a streaming environment without proper synchronization. You encounter a problem when the HashMap memory is corrupted, and because of this, the thread is most likely spinning. You cannot update an unsynchronized card and read it with multiple threads. In some situations, you can create a read-only map and then share it without synchronization in multiple streams.
I would suggest using ConcurrentHashMap instead or wrapping HashMap Collections.synchronizedMap(...) .
To clarify, the problem is doubled here. You cannot have two streams updating an unsynchronized map due to race conditions when changing internal map data. Locking is necessary to ensure the mutex and proper data synchronization. One thread can make changes that another thread has not seen that could overwrite them.
Another problem is memory synchronization. If one thread updates the HashMap in its memory, other threads will not necessarily have the same kind of map storage. This is not a problem until the thread receives a partial memory update - where some HashMap memory is updated and the other parts are not. For example, you can get part of the bucket array or part of the bucket storage, which when moving makes the stream rotate.
One of the main reasons multiprocessor boxes work faster with streaming code is that threads can use cached memory on each processor. The problem with cached memory. One processor can read or modify its cached memory, while the other processor does the same. Synchronizing your local cached memory with central storage is one of the things you need to worry about and the reasons why synchronization is so important.
If you use a pre-populated HashMap that will only be displayed by your threads and will never be updated, this may be ok. I greatly depend on how each of the threads got a link to the new HashMap . If the HashMap was designed and then populated and streamed through their constructor (or before they are launched), then you are kind. However, if the streams are already running, it depends on how they get the map link. They may still receive a partial copy of the cardโs memory, depending on the circumstances and your memory architecture.
source share