🖕 welcome to pay attention to my public number “Tong Elder brother read source code”, view more source code series articles, with Tong elder brother tour the ocean of source code.


Introduction to the

This article focuses on how to read the source code of the JDK itself. We will discuss the source code of various frameworks together later.

The author thinks that reading source code mainly includes the following steps.

Set a goal

Everything has a purpose, so does reading source code.

In a big sense, the purpose of reading source code is to improve our technical ability, apply it to work, quickly locate problems, get promoted and pay rise and so on.

From a small point of view, the purpose of reading a piece of source code is to understand its principle, is dead knock, is the kind of stubborn search for the truth.

The goal is abstract, the goal is concrete, we must give ourselves a goal before reading the source code.

For example, ConcurrentHashMap, which we will study together in the next chapter, we can set the following goals:

(1) Familiar with the storage structure of ConcurrentHashMap;

(2) Familiar with the implementation process of main methods in ConcurrentHashMap;

(3) Explore new technologies emerging in ConcurrentHashMap;

Ask questions

With a goal in mind, we try to ask some questions.

Again, taking ConcurrentHashMap as an example, the author raises the following questions:

(1) Are the data structures of ConcurrentHashMap and HashMap the same?

(2) When will the concurrency security problem occur in HashMap in multi-threaded environment?

(3) How does ConcurrentHashMap solve the concurrency security problem?

(4) Which locks are used by ConcurrentHashMap?

(5) How is the expansion of ConcurrentHashMap conducted?

(6) Is ConcurrentHashMap strongly consistent?

(7) What problems cannot ConcurrentHashMap solve?

(8) ConcurrentHashMap is different from HashMap in other ways besides concurrency security. Why is it implemented in this way?

(8) What are some unusual techniques worth learning from ConcurrentHashMap?

How to Ask questions

A lot of people say, I know you have to ask questions, but how do you ask questions?

This is indeed a very difficult thing, and I think there are three main points:

1. Ask yourself

Put yourself in the interviewer’s shoes and ask yourself questions.

If you can’t ask yourself a few questions, don’t worry, see below.

(2) Ask the Internet

Many of the questions you may not think of, you need to go online to check the relevant blogs, to see if people have raised any questions.

Or, look up relevant interview questions.

For example, ConcurrentHashMap is implemented in the same way as ConcurrentHashMap in jdK7. What improvements have been made to JDK7 in JDK8?

(3) Constantly finding problems

In the process of reading source code, it is very common to see a problem, and this problem should be reserved for research.

For example, how is the size() method implemented in ConcurrentHashMap? What the hell is @sun.misc.Contended? Then check online, and is to avoid false sharing, I X, false sharing is what? And then you look at pseudo-sharing, and you see CPU multilevel caching, right? After learning about CPU multi-level caching, do you feel very similar to the MEMORY model of the JVM? After all these questions, does the world feel clear? ^_^

See, the problems keep coming up.

Therefore, it doesn’t matter if you can’t ask a few questions at the beginning, the key is to look, and then you can find more problems.

Read the source code with questions in mind, ignoring unnecessary details and focusing on important ones

First, be sure to read the source code with questions in mind.

Second, be sure to ignore unnecessary details.

Again, be sure to knock out important details.

At first glance, the latter two steps may seem contradictory, but they are not. Ignoring unnecessary details is to avoid getting lost in the world of source code, and delving into important details is to get to the bottom of the source code.

Details here are ignored or dead knock, mainly depends on the relevance of the problem.

JDK source code or relatively easy to read, if the back of the spring source code, do not ignore unnecessary details, really will be lost, first buried a hint of ha ~~

For example, you read about the readObject() method in the serialization code for ArrayList earlier.

s.readInt(); What do you do for a living? Can you omit it? It’s time to learn about serialization and take a look at the writeObject() implementation, which is the code to write.

SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity); What do you do for a living? At first glance, it looks like this is permission-related code that has nothing to do with our serialization problem, so ignore it, and if you really want to know, just put a flag on it and wait until serialization is solved.

private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
    // Declare an empty array
    elementData = EMPTY_ELEMENTDATA;

    // Read non-transient non-static property (read size property)
    s.defaultReadObject();

    // The number of elements to read is not useful, just because the size property is written, and the order of reading is also read
    s.readInt();

    if (size > 0) {
        // Calculate the capacity
        int capacity = calculateCapacity(elementData, size);
        SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity);
        // Check whether the capacity needs to be expanded
        ensureCapacityInternal(size);
        
        Object[] a = elementData;
        // Read the elements in sequence into the array
        for (int i=0; i<size; i++) { a[i] = s.readObject(); }}}Copy the code

Do more comparison

When reading the JDK source code, it is also very important to do more comparison, comparison can also be divided into horizontal comparison and vertical comparison.

(1) Horizontal comparison

Is to compare with similar classes. For example, the collection module is basically a variety of insert, query, delete elements, then at this time can be compared from the data structure, time complexity and other dimensions, this is horizontal comparison.

(2) Longitudinal comparison

A comparison can be made from the history of aggregate development. For example, the history of HashMap has gone from a (single array) implementation (yes, you can implement a HashMap directly with an array), to a (majority group + list) implementation, to a (majority group + list + red-black tree) implementation in JDK8. This is vertical comparison.

Do more experiment

The last step, the most important, the most important is to do more experiments.

For example, is ConcurrentHashMap strongly consistent?

You can start multiple threads to repeatedly call get(), put(), and size() methods to see if they are consistent.

eggs

Oops, accidentally revealing the next chapter ConcurrentHashMap.

Try reading the source code for ConcurrentHashMap using the methods described in this article, and we will study it together in the next chapter


Welcome to pay attention to my public number “Tong Elder brother read source code”, view more source code series articles, with Tong elder brother tour the ocean of source code.