Introduction to the

Hello everyone, I am Tong Brother, today I would like to talk with you about JDK source code several questions:

  • Why read the JDK source code
  • Read the JDK source code in order
  • JDK source code reading method

Why read the JDK source code

First, JDK source code is the basis of all other source code, read the JDK source code to see other source code will achieve twice the result with half the effort.

Two, JDK source code contains a lot of data structure knowledge, is a good data structure to learn, such as linked list, queue, hash table, red black tree, jump table, bucket, heap, double-ended queue and so on.

Third, JDK source code contains a large number of design patterns, is a good way to learn design patterns, such as adapter mode, template method mode, decorator mode, iterator mode, agent mode, factory mode, command mode, state mode and so on.

Third, the JDK source code contains a lot of high-level Java knowledge, such as weak references, Unsafe, CAS, locking principles, pseudo-sharing, etc., which is difficult to learn without looking at the source code.

This may be the initial idea of many students. In fact, if you really read more source code, this point may not be too important, because you will find a wider world.

Five, tong Elder brother think the most important, reading source code is a kind of exercise of thinking, is the best way to learn excellent design, this article comes from the number tong elder brother read source code.

Read the JDK source code in order

First, THE JDK source code is divided into the following sections:

The base class

Base classes are the classes that make up the base of JDK source code.

For example, wrapper class, reflection class, tool class, etc., these classes have one thing in common, that is, the code logic is relatively simple, there is no data structure, complex operations and other problems.

For basic classes, Tong Ge’s advice is to read them from beginning to end, and write test cases or look up information online if you don’t understand. For example, if Integer has an IntegerCache class inside it and you don’t know what it’s doing, you can’t just look it up on the Internet.

Simple set

Simple collection refers to the collection that does not have multithreading security problems.

These collections are typically used in single-thread or method bodies, but they use a lot of data structures, so some knowledge of data structures is required.

For simple collections, Tong Ge’s advice is to understand the underlying data structure knowledge, and then look at the source code, which may be easier. Of course, there will be data structure series in tong Ge.

Atomic classes

Atomic class refers to the class that can guarantee atomicity in multi-threaded environment.

This category mainly includes classes beginning with Atomic* and ending with Adder, which are located in the Atomic package under JUC.

For atomic classes, Tong’s advice is to look first at low-level concepts such as Unsafe, CAS, and pseudo-sharing, then at the simplest AtomicInteger, and finally at complex classes such as LongAdder. Among them, breakpoint debugging is indispensable means.

To be honest, the LongAdder class provides a lot of advanced learning, and it is highly recommended that you explore it thoroughly before looking at the source code for Disruptor, Netty, etc.

synchronizer

Synchronizer, refers to the control of multiple threads of competition and the existence of the class or keywords, this article is derived from the number of tong brother read source code, they can be said to be the most important content in Java, without them can not control the normal operation of multithreading.

This part mainly includes synchronized keyword, volatile keyword, reentrant lock, read-write lock, timer, semaphore, loopback fence, stager, distributed lock and so on.

For synchronizers, His advice is to learn the basic concepts of memory model, visibility, atomicity, orderness, happens-before, try reading the source code for this section, and then generalize your own “synchronizer principles.”

Concurrent collections

Concurrent collection refers to the collection that can ensure data consistency in multi-threaded environment.

This part of the collection is mainly used in the multi-threaded environment, only a few classes involve advanced data structures, more are the use of locking, CAS, volatile, spin and other advanced skills.

For concurrent sets, Tong Ge has three suggestions:

  • Be sure to read behind the synchronizer
  • Figure out data structures first, like ConcurrentSkipList
  • Using Thread level breakpoints of IDEA, debug, debug, debug

Thread (pool) class

Thread (pool) classes refer to classes related to threads and thread pools.

This category includes Thread, ThreadLocal, three Thread pools, and so on.

For thread (pool), red elder brother suggestion is first on the whole grasp, and then divided into several blocks, look at things which just look at that piece of thing, don’t tube other code, i.e. to find out where your focus is, for example, look at the state of the thread running processes don’t tube, all involving state code all over, and vice versa, all finished, string together again.

IO/NIO classes

IO classes refer to the classes related to input and output streams. This category mainly includes the classes related to file operations and network IO classes.

For THE IO class, Tong Elder brother’s advice is to simply browse, do have a good idea of it, and then check it when you use it.

However, niO-related classes should be studied carefully, and we will study this category together in the related section of Netty source code reading.

Other classes

For other classes, you can click on them in your work, but it is not recommended to take time out to study them alone. For example, time class and AWT class are not very necessary to see.

JDK source code reading method

First, set goals, as clear as possible, do not set too vague. For example, familiarity with the data structure of a HashMap is an obvious goal; Reading the source code for HashMap, for example, is quite illusory.

Two, try to put forward their own questions, first according to a knowledge point divergent questions. For example, what knowledge points can you think of about HashMap? You can use mind map to imagine infinitely. Later, there will be an opportunity for Tong Brother to share his mind map ideas with you.

Three, try the network query problem, open Baidu, input you want to learn knowledge points, the first few pages all open, see others have encountered what problems, of course, the ability of the students can also use Google, this part of the query can be added to your mind map.

Four, try to read the source code, for the above problems, one by one to try to find the answer in the source code, by point and surface, and finally summarize the whole big knowledge point.

Five, constantly find problems, in the process of reading the source code may find new problems, first skip, but add it to the mind map, and so on the current problem solved to solve.

Six, focus on your problem, when reading the source code must focus on your current problem, do not be distracted by other problems, such as looking at the thread pool task execution process, you do not care about the thread pool state.

Seven, do more comparison, horizontal comparison and vertical comparison, from multidimensional to compare, this article comes from the number tong Elder brother read source code.

Eight, do more experiments, more use of IDE debugging mode, constantly modify breakpoints, debugging.

Nine, more communication with people, if the conditions allow, more communication with the surrounding people, of course, can also come to harassment tong elder brother.

Ten, make more summary, for their own solution of the problem, must learn to summarize, make more study notes, of course, welcome to tong Brother here to contribute.

Eleven, patience & persistence, reading source code is a very boring and boring thing, must insist on insist on.

conclusion

Today, we talked about learning JDK source code several problems, accidentally wrote so much, are words from the heart, I hope you can accept my knee.

eggs

Finally, send a mind map of JDK source code, star is recommended to see the part of Tong Elder brother, for students with enough time, no star is of course best to see.

Part of the JDK source code tong Elder brother has been all resolved, click the public number tong elder brother read source code lower left corner “JDK source code” can view all the source code analysis.