Cloud rendering servicesjava.util.ConcurrentModificationExceptionAbnormal problem
Reflect on and review a recent problem in cloud rendering services
Callback video rendering progress problem
- After modifying the URL for the callback progress, the previous code snippet is enabled. In multithreaded programming, using hashMap directly to save the current rendering task without doing some of the necessary mutual-exclusion and synchronization between threads causes one thread to iterate over the HashMap through an iterator while another thread modifs the hashMap, causing the program to throw
Java. Util. Abnormal ConcurrentModificationException Java. Util. ConcurrentModificationException ats java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1493) at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1516) at com.seeshion.schedule.ProcessTask.test(ProcessTask.java:39) at jdk.internal.reflect.GeneratedMethodAccessor32.invoke(Unknown Source) … The program does not handle the exception, resulting in repeated task failures
The solution
-
HashTable
We can use HashTable to solve this problem. HashTable<K, V> is also a key-value HashTable. It operates in much the same way as HashMap, except that HashMap is not a thread-safe class, and concurrency conflicts can occur in a multi-threaded environment. However, HashTable is a thread-safe class. In order to achieve multithreading safety, HashTable uses synchronized to lock the whole Hash table, that is, to achieve thread security by locking the whole table each time. HashTable adds locks on almost all methods, but it also leads to low operation efficiency. In a cloud rendering service scenario, this time is negligible.
-
ConcurrentHashMap
ConcurrentHashMap ConcurrentHashMap is a more efficient concurrent container than HashTable. It uses the lock separation technique, that is, multiple locks are used to lock different parts of the Hash table. These different parts are called segments. Each segment has its own lock, and as long as multiple concurrent operations occur on different segments, the modification operation can be executed truly concurrently