HashMap in Java causes cpu100% problem in multi-threaded environment

Source: Internet
Author: User

Recent projects in the case of Tomcat occupy cpu100%, the original thought is a dead loop in the code, the background using jstack did dump, found that the system unreasonable use of hashmap led to a dead loop (note is not a deadlock).

The cause of this dead loop is the operation of an unprotected shared variable-a "HASHMAP" data structure. When the "synchronized" is added to all the methods of operation, everything is back to normal.

Is this a JVM bug? It should be said that it is not, this phenomenon has been reported long ago (see more: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6423457). Sun's engineers did not think it was a bug, but rather suggested that "concurrenthashmap" should be used in such a scenario,

Reply to the exact words:

This is a classic symptom of the incorrectly synchronized use Ofhashmap. Clearly, the submitters need to use a thread-safehashmap. If they upgraded to Java 5, they could just use Concurrenthashmap.

So in the development process should pay attention to this, in the multi-threaded environment, try to use Concurrenthashmap.

There may be problems in the area of expansion

Java code
  1. void Resize (int newcapacity) {
  2. entry[] oldtable = table;
  3. int oldcapacity = oldtable.length;
  4. if (oldcapacity = = maximum_capacity) {
  5. threshold = Integer.max_value;
  6. return;
  7. }
  8. entry[] newtable = new entry[newcapacity];
  9. Transfer (newtable);
  10. Table = newtable;
  11. Threshold = (int) (newcapacity * loadfactor);
  12. }

This method itself is not a problem, the problem is transfer (newtable), this method is used to move the data in the oldtable to newtable.

Java code
  1. /**
  2. * Transfers all entries from the current table to newtable.
  3. */
  4. void Transfer (entry[] newtable) {
  5. entry[] src = table;
  6. int newcapacity = newtable.length;
  7. For (int j = 0; j < Src.length; J + +) {
  8. //(1)
  9. Entry<k,v> e = src[j];
  10. if (E! = null) {
  11. SRC[J] = null;
  12. Do {
  13. //(2)
  14. Entry<k,v> next = E.next;
  15. int i = indexfor (E.hash, newcapacity);
  16. //(3)
  17. E.next = Newtable[i];
  18. Newtable[i] = e;
  19. e = next;
  20. } while (E! = null);
  21. }
  22. }
  23. }

The following analysis may occur, assuming that the original oldtable stored a1,a2 hash value is the same, then the entry list order is:

P1:oldtable[i]->a1->a2->null

P2:oldtable[i]->a1->a2->null

The thread P1 runs to (1) below this line, E=A1 (A1.NEXT=A2), continues to run until (2), next=a2. This time switch to thread P2, thread P2 execute the loop of this list. If the hash value of the A1,A2 in the new table is the same, then the list order at this time is:

Main Memory: Newtable[i]->a2->a1->null

Note that this time, the A1,A2 connection sequence is reversed. Now the CPU is re-P1, after (3) this line: E.next = Newtable[i]; that is: a1.next=newtable[i];

NEWTABLE[I]=A1;

E=A2;

Start the second while loop (E=A2,NEXT=A1):

a2.next=newtable[i];//is A2.NEXT=A1.

Newtable[i]=a2

E=a1

Start the third while loop (E=a1,next=null)

a1.next=newtable[i];//is A1.NEXT=A2.

This time a1.next=a2,a2.next=a1, forming a loopback, which creates a dead loop, when the get operation, Next is never null, resulting in a dead loop.

You can see that there are occasional cycles of death, but once the consequences are very serious, the multi-threaded environment should be used Concurrenthashmap.

HashMap in Java causes cpu100% problem in multi-threaded environment

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.