There is a HashMap , which contains a list of orders. It may happen that several threads simultaneously try to accept an order.
In order to avoid such a situation, I try to put a lock on a list item (but not on the entire list, so as not to interfere with reading and similar processing of other elements):
Order order = map.get(id); if (order != null && !order.isAccepted()) { synchronized (map.get(id)) { if (map.get(id) != null && !map.get(id).isAccepted()) { map.get(id).accept(); } } } First, I pull out the object and check the required condition on it so that it does not become null during the condition check. Then I block this item in the list and re-check the condition (it could have changed during the test of the first condition), after which the action is taken.
Is it worth using the blocking of one element, does it really allow working with other elements without blocking the list and does this blocking cause problems (for example, during deletion)?
map.get(index)inside thesynchronizedblock returnsnull, then theNullPointerExceptionwill end. If another object is written betweensynchronizedand the internalifinmapusing theindexkey (strangely, the key is called the index), thenacceptwill be called for an object that is not associated with the synchronized object. - Regent