<> Why read JDK Source code

Of course not to pretend , After all, who has nothing to do with it …

1, Interview can't run away . Now just an interview Java Relevant positions , More or less JDK Source related issues .

2, Understand the principle without panic . We as JDK Users , Although I'm happy every day , But sometimes you have to go with the underlying source code to see if you have any problems , To help us understand the principle better ,

3, Learn good code , Ideas and models
.JDK After all, it's an excellent code base , We use it every day , Source code is also in it , As a aspiring programmer , Reading the source code can also let us absorb more excellent ideas and patterns .

4, Hypnotic sleep . forehead …… But it works ( funny ).

<> Is source difficult? ?

Crap , Of course, it's difficult , Otherwise, I won't have seen it all by now , And I often forget it , Cry and chirp …

After all, like JDK This source code , And we usually practice handwriting , Write business code differently , After all, they are Class library , For performance , stability , Generality , Consideration of expansibility and other factors , Added a lot of auxiliary code , generic paradigm
, And some design pattern considerations , So it certainly doesn't look so easy , I can't see through it at a glance .

So it must be a long process , My personal advice ( Including myself ), Sometimes I have some problems , The source code of some components or parts can be targeted , Go to the bottom , And take notes , Write some notes , So it can penetrate into a lot of content .

But we must have enough confidence , I believe that the code has been written , I don't believe it. I can't understand it !

<> How to see the source code

1, Method 1 : Reading on demand
. If for a component , Grammar or special interest , Or some questions , Can be targeted with the underlying source code to view on demand , It's also more efficient , A fast way to build confidence .

2, Method two : Systematic reading . Specific reading contents and sequence suggestions are detailed below .

3, Multi debugging : If you look only through your eyes , Then it's hard to debug the brain patch , It's better to use IDE Hands on debugging , Just take two steps .

4, Don't just read it. , Remember to leave something after reading . I don't think it matters how much I read , What matters is how much output , Sum up , Induce , Write notes , Note taking

So I'm going to build a Java Source reading and debugging environment , One for each , Whenever there's a whim , When there is a problem , When in doubt , When you are bored, you can open the project and have a look at the source code , Make notes and notes .

<> Build a source reading and debugging environment

Personally, I think it's better to do one thing alone when looking at the source code Java engineering , Source code in , Test code is also put in , Centralized debugging , Focus on code , It's more convenient to write notes intensively .

1, Create source reading project

Choose the most common Java Basic project :

2, Create two directories

Respectively :

* source: Later placed JDK Source code in
* test: Place test code , It can also create hierarchical subdirectories as required

3, Import JDK Source code

A lot of friends asked JDK Where is the source code of ?

be as far as the edge of heaven , Only in front of eyes , In fact JDK You can find it in the installation directory .

JDK There is a directory named src.zip Compressed package , That's exactly what it is. JDK Source code !

Extract it and copy it to the source Directory , such JDK The source code will be imported .

Some of you have questions , Why would you JDK Source guide a copy into this project ? In fact, the main reason is that it is convenient for us to read in the source code , debugging , And make notes and notes .

As for this JDK How to use the source code , The following is continued .

4, Commissioning and operation

We can test Write a test code in the directory at will .

For example, here I am HashMap take as an example , stay test Create a subdirectory under the directory hashmap, Then create a test main entry file in it Test.java, Put a piece of test code at will :
public static void main( String[] args ) { Map<String,Double> hashMap = new
HashMap<>(); hashMap.put( "k1", 0.1 ); hashMap.put( "k2", 0.2 ); hashMap.put(
"k3", 0.3 ); hashMap.put( "k4", 0.4 ); for ( Map.Entry<String,Double> entry :
hashMap.entrySet() ) { System.out.println( entry.getKey() +":" +
entry.getValue()); } }
Then start debugging .

But there will be several problems to be solved one by one .

Question 1 : Start debugging Build Report errors , Prompt for insufficient system resources

Resolvent : Enlarge Build process heap size.

Setup method :Preferences --> Build,Execution,Deployment --> Compiler, Default 700
Increase the value of , For example, I set it here as 1700:

Question two : Want to start from outer code F7 Single step debugging entry JDK Source code , It turns out I can't get in

This is because when debugging ,JDK Source code protected , Generally, single step debugging is not allowed , But it can be set .

Resolvent ::

Preferences --> Build,Execution,Deployment --> Debugger --> Stepping

Question three : How to JDK Annotate the source code ?

Debug access JDK After source code , Unable to comment found , There is a small lock icon on each file , This is because the current associated source code is not the source code just copied into our project , But JDK Under the installation directory
src.zip Read only compressed package .

Terms of settlement : Re Association JDK The source path is the copy of this project path JDK Source code .

Thus , We can be happy to JDK Source debugging and annotation .

<> Source structure and reading order

JDK After all, the source code is too large , It's not realistic , We would like to take a look at the important content first according to the frequency of daily use and interview investigation .

If you don't have a special plan , You can follow the recommended reading sequence as follows :

The specific contents are as follows :


It's actually Java Basic grammar of , For example, various basic packaging types (Integer,Long,Double etc. ), Basic class (Object,Class,Enum,
Exception,Thread) Wait …


Contain Java Annotate source code related to basic elements


Contain Java Reflect code related to base elements


It's all there Java Basic tools of , The most typical and commonly used are various containers and collections (List,Map,Set)


Famous JUC package , It contains Java Concurrent and multithreaded programming related code

6,java.util.function +java.util.stream

Contain Java Common interface and code of functional programming


Contain Java tradition I/O Related source code , Mainly byte and stream oriented I/O


Contain Java Non blocking I/O Related source code , Mainly for buffering , Channel and selector I/O


Contain Java New date and period related codes , The most typical is LocalDateTime,DateTimeFormatter etc.


It mainly includes some supporting data classes of high-precision operation


Mainly include Java Network Communications ( Typical such as :Socket Signal communication ) Related source code .

<> Mutual encouragement

Look at the source code. It can 't be rushed , Slow down to be faster ! Mutual encouragement .

©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train SparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network c++ Memory Pointer out of bounds detection mechanism _CrtMemBlockHeade Change one's mind ! Tesla starts to deliver made in China to European market Model 3Qt study 7—— Modal and modeless dialog boxes JavaScript Medium Call and ApplyJAVA Convert a string to a numeric type k8s Entry to give up --k8s Key concepts Count the number of letters (java Language implementation )