The revelation of the dead cycle based on Java HashMap detailed _java

Source: Internet
Author: User

One, single-threaded transformation for multithreading is also a technical work

As we can see in the mouse Uncle's blog, the original is a single-threaded application, "later, our program performance problems, so we need to become multithreaded, so, become more than a thread after the line, found that the program often accounted for 100% of the CPU."

Take into account is the Taobao engineers exposed the problem, their technical basis is generally very solid, even they are using the wrong, so the single thread to transform into multithreading is not imagined so simple, I think.

You may be very unconvinced to ask, Taobao engineer and how, single-threaded to multithreading what is difficult? is to apply the existing multithreading technology, you see, I have a very strong sense of thread safety, I know synchronization, deadlock, race conditions, but also know the lock free and thread-safe container, but also know the various thread-safe synchronization constructs ... Do you write applications that are not thread safe?

The reality is that thread-safe applications are not necessarily written because you have a solid thread-safety foundation and development experience.

Try to give two examples:

1. Using a thread-safe container to fetch data by index

Many people know that the thread-safe container, the actual use of the time does not necessarily not appear bugs, the following (hidden) code is more typical:

Copy Code code as follows:

static int Getfirstordefault (threadsafelist<int> list)
{
if (list. Count > 0)
{
return list[0];
}
return 0;
}

The function parameter list above if you start by passing in a list of elements with a total of 1, can you analyze what is wrong with the above code?

About the thread-safe container, I just happened to summarize an article < the implementation method > of thread-safe container . The thread-safe container is not really secure, and the problematic code is here.

2, multithreading operation of the mail error

There is a multi-threaded application scenario analysis may not be correct, once because of a mail transceiver performance problems, I also boldly modified the application, changed to change to appear a major bug,

You can look at my painfully summed up < based on an application multithread misuse analysis of detailed >.

For the two examples above, I just want to show that in multithreaded applications, because thread safety bugs are actually very subtle, a lack of thought or understanding, the likelihood of problems is impossible.

Second, the cost of rehash

The 1th above is mainly chat thread safety, and then we also talk about hash tables, a deep understanding of the cost of consumption rehash.

The hash table We normally understand is "a data structure that changes time in space." This is too long to say, we may have an intuitive illusion, that is, the hash table is the space to fight for time.

However, the process of rehash is actually a double significant loss of space and time, because the analysis of the source code, we know that the process of rehash is a dynamic expansion of the process, and the expansion of the hash table is a space and time consumption is very amazing internal operations.

Why is it that rehash is a very astonishing internal operation of space and time consumption?

1, the original when we expand the container of the hash structure, the hash table to new a larger array, and then copy the contents of the original array to the new array, and to hash;

2. It is also a matter of knowledge as to the size of this larger new array, which, in general, will be set to a prime number similar to the double size of the original array. NET in the generation of this prime number there is a certain skill.

From 1 and 2, it can be seen that the cost of rehash is indeed very high. Not so long ago I happened to have written an article about. NET container dynamic expansion of the article < resolution from the source analysis of the common data structure based on array dynamic expansion mechanism of the detailed explanation , which is also a simple summary. NET's hashtable expansion mechanism, now contrasts the Java HashMap source code, sees the familiar rehash function name, again looks again. NET in the implementation, indeed there is a comparison can be improved.

As for our usual understanding of "space for Time", actually refers to the hash has O (1) Complexity of the data retrieval efficiency, but it is affected by the fill factor, space overhead is usually very large, space utilization is not high.

So we often say that the hash table is suitable for frequent read operations, less application scenarios for write operations, such as a hash table as a buffer container, in my heart.

Finally, "someone gave the question to sun, but Sun didn't see it as a problem." Because HashMap does not support concurrency. To be concurrent, use concurrenthashmap ... "

According to the actual development experience, thread-safe container is not really thread-safe, will use Concurrenthashmap also just enter the initial stage, at the same time can not help but feel the glory of the sun under the boundless scenery.

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.