HashMap Non-threading security issues encountered in the production environment java.lang.thread.waiting

Source: Internet
Author: User

Written in front: work for several years, from the introduction to the present, encountered also solved some problems. (Of course, the framework level is not yet) has been a variety of fix methods from the blog park and other major communities. At present a little leisure time, after looking at the Big V Shenjian blog post, I also summon up the courage to write a blog, record the work encountered and resolved problems (including of course, I am in the Bo Park to obtain a variety of solutions; can find Yumboven of the younger brother will indicate the source. Because always feel that their level is not enough, afraid to write out the article misled others. The following is a problem in the production environment this week, write out for your reference. Phenomenon

Week 51 early in the morning, the car did not stop steady (electric cars), the group began to ask who the recent code has more time-consuming performance of the operation, everyone said no. And then there's a picture:

The CPU has soared to 1600 +, the problem is still more serious. The OPS group is then contacted to give the Tomcat run log to troubleshoot for errors. After the troubleshooting log, send the following error:

See after the rapid sitting to position, to query the corresponding error code;

Analysis and search:

Here's some code:

1      Public StaticMap<string,string> Citycodemap =NewHashmap<string, string>();2     3      Public StaticMap<string,string> Provincemap =NewHashmap<string, string>();4     5      Public StaticList<string> sensitivewordlist =NewArraylist<string>();6     7      Public Staticstring Getcitycodebynumber (string phonenumber)8     {9String start = stringutils.substring (PhoneNumber, 0, 7);Ten         if(Stringutils.isempty (start)) One         { A             return"000"; -         } -map<string, string> citycodes =getcitycodes (); theString Citycode =citycodes.get (start); -         if(Stringutils.isempty (citycode)) -         { -             return"000"; +         } -         returnCitycode; +}

Mainly used to get the number of mobile phone number segment corresponding to the city code, a module of the project (not to advertise it) need to use. And every call is used; At first glance, I feel no problem, so many places in the project are doing so. Then run the code in the test environment and the local environment, all running normally.

Then I searched for java.lang.thread.waiting, and in this post I found some questions, http://www.cnblogs.com/zhengyun_ustc/archive/2013/03/18/tda.html

It is possible that the problem is caused by multithreading.

Here is a list of two more detailed blog posts, the source tracking and analysis process is very detailed.

http://coding-geek.com/how-does-a-hashmap-work-in-java/

Https://coolshell.cn/articles/9606.html

  

Although this is a probability, it is also dangerous in the case of a large concurrency ratio. If it is not found, it is likely to result in a single point of failure even if the entire cluster is unavailable. And according to the situation of their own project code analysis, mainly because in the initialization, the loop to the map put new elements caused by the map expansion rehash generated a dead loop.

Initialization code:

     Public Static Map<string,string> getcitycodes () {        if(Citycodemap.isempty ())        {            Set <String> KeySet = Resourcebundle.getbundle ("Config/citynum"). KeySet ();              for (String key:keyset) {                = Propertiesutil.getkey ("Config/citynum", key);                Citycodemap.put (key, Citycode);            }        }         return citycodemap;    }

PS: There are tens of thousands of key-value pairs in the citynum here.

Workaround:
    1. Hashtable
    2. Concurrenthashmap
    3. Synchronized Map

Can self-search implementation principle, a lot of great God, big fairy Son are elaborated than I detailed.

All say programmers are lazy people, I do not agree. We just want to use the least amount of code to solve the problem. So, our improvement plan is to change the HashMap directly into Concurrenthashmap.

HashMap Non-threading security issues encountered in the production environment java.lang.thread.waiting

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.