In a multi-threaded Java application, I need to iterate over a collection of objects. Since both the collection and objects can be modified by another thread, while I repeat them, I need to use synchronization.
However, nested synchronized blocks are not recommended because they can lead to deadlocks. How can I solve this problem?
Collection<Data> dataCollection = something.getDataCollection(); synchronized ( dataCollection ) { for ( final Data data : dataCollection ) { synchronized ( data ) { data.doSomething();
java multithreading synchronization nested
Sven jacobs
source share