Thread-dead loop problems caused by using HashMap in concurrent environments __hashmap dead loops

Source: Internet
Author: User

Recently and maintained a colleague to locate the product's online problem and encountered a thread-stuck case. The ultimate root is that caching is implemented through HASHMAP

, and the synchronization mechanism is not used. In a multithreaded environment, the update/Get HashMap value, causing the thread to enter the dead loop. The JDK document explicitly says that HashMap is not thread-safe, so using HashMap in multiple threads without any lock is a serious mistake in itself.

The individual has a little bit of detail and wants to know why it causes the thread to die loops. In the process of studying the reason, I found a very good post, very vividly explained the generation of the death cycle, illustrated. Vaccine: The dead cycle http://coolshell.cn/articles/9606.html of Java HashMap. Here's a note, as your own memo.

and http://yananay.iteye.com/blog/910460 this blog, a good description of the HashMap source indexfor skills

/**
     * Returns index for hash code h.
     *
    /static int indexfor (int h, int length) {return
        H & (length-1);
    }

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.