Java.util.ConcurrentModificationException

Source: Internet
Author: User

The most recent system memory leak, the discovery is a framework of the clear resources of the thread abnormal exit caused, the reason for the exit is to throw a concurrentmodificationexception.

When to throw concurrentmodificationexception?

This exception are thrown by methods that has detected concurrent modification of an object when such modification is Not permissible.

This explanation is actually quite unclear, what is called without permission when the modification ah, what the situation ah. It is now known that you should pay special attention to the changes in collection traversal.

Http://stackoverflow.com/questions/8189466/java-util-concurrentmodificationexception

Throw exception code as follows:

1 Static2     {3Thread th =NewThread ("Shycachescanthread"){4              Public voidRun ()5             {6                  while(true )7                 {8                     Try{Thread.Sleep (default_check_interval);}Catch(Interruptedexception e) {}9                     synchronized(All_cache)Ten                     { One                          for(Abstractcache cache:all_cache) A                         { -                             intx = Math.min (Cache.mcapacity/2,cache.mmap.size ()-cache.mcapacity); -                             if(X > 0 ) the cache.evict (x); -                         } -                     } -                 } +             } -         }; +Th.setdaemon (true); A Th.start (); at}

Where All_cache is a list object, in fact, this code does not have any problem, but to All_cache do the traversal, but here to be particularly careful, in the other places to modify this object must also be locked, but unfortunately

1  PublicAbstractcache (intcapacity)2     {3         if(Capacity <= 0 )4             Throw NewIllegalArgumentException ("Capacity <= 0");5Mcapacity =capacity;6MMap =NewInthashmap (capacity);7All_cache.add ( This);8}

Changes to the All_cache are not locked here, and once the Abstractcache is constructed, the shycachescanthread is traversed, and the exception is thrown out.

The workaround is to add the All_cache object lock at Add, but it is not elegant. I am puzzled about why the framework of the cache implementation to look like this, alas ~

The problem is not over yet ~

Why hasn't it happened before and it's been happening lately? Because the code above is only run once during initialization, there is no modification.

Java.util.ConcurrentModificationException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.