In my previous work experience, ArrayList and HashMap are the most commonly used collections. ArrayList and HashMap types are not thread safe, but the actual code does not encounter the scenario of multithreading ArrayList and HashMap operation, resulting in App crash. Therefore, even if it is seen, there is no experience, will not be at ease in the mind.

When the company was in charge of the development of the video chat function this time, it was also habitual to use ArrayList and HashMap to temporarily cache data at the beginning. I didn’t find this bug before the business was very complicated: java.util.ConcurrentModificationException

As the function improved, the test data of the account kept piling up, and I had to deal with a huge amount of disgusting offline data, I was really driven crazy, all kinds of crashes, the whole person was not good. All kinds of consult god, looking for information, all kinds of attempts, and finally this disgusting bug is also solved.

Now, without further ado, I will summarize my bug fixing experience:

1. Suggestions in Bugly: Use ConcurrentHashMap to replace HashMap and CopyOnWriteArrayList to replace ArrayList

Conclusion: CopyOnWriteArrayList fixes my bug: Java. Util. ConcurrentModificationException but also caused the new bug, is update not in time, because the cause of the lock, lead to multi-threaded operation, be locked side, the side has been updated UI, lead to get the data is not up to date.

I don’t understand the CopyOnWriteArrayList object very well, but I recommend caution when working with multiple threads.

2.Vector Vetor is an updated version of ArrayList that is thread-safe. Listened to the great god suggestion, looked for relevant data on the net, stick next

I was pretty freaked out, but I tried the following to find and fix my bugs

* * 3. The key here, finally I use Collections. SynchronizedList, perfectly solve my problem, and there is no dig a new hole, it did not, of course, other bugs.

Conclusion: Recommend!! 支那

About multithreaded operations HashMap collapse due to the App, also reported the bug: Java. Util. ConcurrentModificationException

4. Final solution: Use ConcurrentHashMap to replace HashMap