"Daniel in the end is how to read the JDK source code? "Together to learn about

Preface:

How to read the source code, is a challenge faced by every programmer need, why you need to read the source code from the practical point of view, there are three main purposes?:

First, solve new problems or new requirements at hand;

Second, part of the floor to really understand the theory of realization;

Third, to deal with the interview.

"Daniel in the end is how to read the JDK source code?  "Together to learn about

 

Correct attitude

Until you are ready to join the cause of reading the source code, you first need to correct what state of mind:

  1. Reading the source code is a protracted war, kick the tires, then, at most, can write a few hydrology, suck powder fool at the novice, there is no significant effect on improving its own power.
  2. Read the source is icing on the cake, not timely. If you enter a new field, the first task is to read the document instead of reading the source code.
  3. Reading the source code does not necessarily make you a great God. Great God in two directions: expert and applied, regardless of which side you favor the development of a large number of practical operation is the most fundamental guarantee.
  4. Reading the source code needs theory first. Socket Communications such as reading the code but do not know TCP / UDP protocol, it's like headless flies only in the wild guesses.

Here, I recommend reading as the source is a hobbies do, like some people relax by playing a game to see live, some people chase through the brush news hot play relax, and some people find friends by bragging force relax ... different people choose different ways of work and rest, I prefer not to write code, to relax by watching other people's code.

As a Javaer, daily life is a natural treasure JDK place at your first encounter. JDK starting from a reading of the code, in depth understanding of some of the old and new features of Java, and part of the design patterns learning applications, as well as to lay a solid theoretical foundation and tenacious psychological foundation for future reading more bulky frame.

Selection Tool

工欲善其事必先利其器, before starting, you need to choose a source code reading tools. In the choice of tools, a lot of peer advice, I generally will be divided into four categories:

  • Text-based tool (the classification may be controversial, but that's not the point ...)
  • For example Nodepad ++, EditPlus, UEStudio, Sublime, VsCode, Vim, etc.
  • Expert Tools
  • For example Source Insight, Understand, OpenGrok (also a cornerstone of many online tools), etc.
  • Online tools (several online sites has hung up)
  • For example openjdk, SearchCode etc.
  • HERE
  • E.g. eclipse / myeclipse, IDEA, etc.

Speaking from personal preference, I recommend IDEA and UEStudio (with UltraFinder) used in conjunction.

IDEA Java as a powerful production tool used to read the Java source code is clearly appropriate. UEStudio can choose when and as temporary files or view the Java JDK view in section C ++ code, and then with UltraFinder, arbitrary symbol search across files, is very practical.

Read about the build environment, see I can describe in https://github.com/kangjianwei/LearningJDK in.

Reading Order

JDK project over a dozen large version, count the contribution of the open source community, handling people may also reach thousands of people. For this huge project, a one-time reading is certainly not possible, you must first find an appropriate entry points to a little Kenwan module.

One possible reading order is:

  1. The basic types of packaging (Character placed at the end)
  2. String, StringBuffer, StringBuilder, StringJoiner, StringTokenizer (regular expression of knowledge supplement)
  3. CharacterIterator、StringCharacterIterator、CharsetProvider、CharsetEncoder、CharsetDecoder(较难)
  4. Function expression in java.util.function
  5. Buffer under various java.nio achieve
  6. And various references in java.lang.ref jdk.internal.ref: soft references / weak reference / references virtual
  7. Unsafe implementation (there are two classes of the same name after JDK9, a reference to another, recommended read together)
  8. Flow programming implemented under java.util.stream (hard)
  9. Thread和ThreadLocal
  10. Math、Random、BigInteger、BigDecimal
  11. java.lang.reflect reflection implementation (first master module system is introduced after 9 JDK)
  12. ClassLoader implementation
  13. Implementation of the Java language model under javax.lang.model (refer to Java syntax official document)
  14. Notes (required to completely master)
  15. Timer、ResourceBundle、Properties
  16. The date and time types (especially Java8 new section)
  17. java.lang.reflect.Proxy, JDK dynamic proxy default
  18. java.util.concurrent and contracting. Read first atomic class, read lock implementation class, and finally achieve those concurrent reading tools (difficult)
  19. Collections Framework, mainly three categories: List, Set, Map (first reading achieve non-thread-safe, read to achieve thread-safe)
  20. Network Programming (main reading Socket communication section, read the subsequent realization of HttpClient)
  21. IO / NIO / BIO (difficult)
  22. Files, Path and other file manipulation tools
  23. sql, xml processing class / interface
  24. ......

Note here that the order is only a general direction, it does not mean that absolutely need to be in accordance with this list.

When reading a code, often involve a lot of other code, the time will have a lot of reading branch to branch, not in the above list.

"Daniel in the end is how to read the JDK source code?  "Together to learn about

 

Make good use of skills

Code reading skills, vary. Like one thousand readers, there are one thousand Hamlet, everyone's view on this matter is not the same. Here, I talk about some personal experience.

Theory first. When reading a module, first search for its theoretical support, or even look at other people's reading experience, with a rough outline after he go practice.

Must Try. The face of a new class, it is best to search its basic usage, write a small example, and start from the method used in this example, to analyze this class.

Using debugging. On the use of IDEA in debug mode, and is beyond the scope of this article describes. Note that the value is, except when needed to learn commonly used to run the debugger, you also need to learn when debugging compiled, this is useful when reading the Java language model piece of code.

Prioritize. Reticular structure between classes, reading in a class when the inevitable need to go read it references other classes. However, if it is referenced class is very complex, it is recommended that you put off, make a mark, read it back. However, if multiple classes in reading, its call chain will eventually point to the same class, the class must first won.

Business first. If a class is too large, the first of these methods are classified by function, stroke clear of approximately execution flow, one by one and then the next function to go to overcome.

Superficial understanding. Some methods do not need to figure out the implementation process, only we need to understand its role. For example, some algorithms in specific areas, such as to resolve certain rules.

From point to area. If you read a certain method, you must search at all applications of the method to verify their ideas are correct, and write comments at the application. Even if there are understandable error is all right, then back to have a new understanding of bulk editing. For the reading and understanding of the field, it is recommended to take this way.

The courage to trial and error. Many describe the interface methods are abstract classes in different implementations vary greatly in significance. At this point Learn the realization of a class, and then took to understand in this class to interpret another implementation class, if the interpretation is wrong, then gradually repaired. Do not expect a one-time will be able to correctly understand the role of a method, misunderstanding, without prejudice to move on.

Pay attention to comments. Most of the disclosed method has a corresponding notes, this is an important way to quickly understand this approach. NOTE It is recommended to get the Google translation continues to read, of course, if you can fluently read English better. But very often, the comment is frustrating: After reading do not know his spade. This is normal, because some of the comments will be involved in many industries or generic term used to describe solutions, without which the theoretical background before the high probability is not read comments. Native Notes is not a panacea, and sometimes very sad: you do not understand this approach before, did not understand his comment, so you understand this method, these notes will feel right. Therefore, I suggest that attention to comments, but do not rely on notes, sometimes search for other users to understand, combined with their reading, they will come more comfortable.

Frequently taking notes. A little inspiration, you need to record what the best position is directly recorded in the corresponding source code, but also a detailed brief on the other, a good memory eventually arrived, but bad written.

Step by step. In the mind awake, open source code to read, feeling not read the time, do not continue Sike, it should lay down doing something else, or another day read. I read a class, the time span may be more than one month, which is constantly replenished and improve the process, not a one-time fix. Sometimes she looks to read, but it is almost critical point to understand, this time people are likely to urgent, urgent it is easy to dry, dry it is easy to panic, it is easy to panic disorder, chaos, it is easy to drop the mouse. So, if they find encounter a problem, it is timely to terminate it, because you may need to relax the brain, as well as complementing the basic theory of some of the missing.

Finally, thank you for your patience to read the article!

Guess you like

Origin www.cnblogs.com/L10086/p/11354996.html