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);
}