Java source code reading experience

Source: Internet
Author: User
Tags jbpm
I think there are three core points in the source code reading: technical foundation + strong curiosity + patience.

Speaking of the technical foundation, let me give you an example. If you have never learned Java or any programming language such as C ++, you should start with core Java. it is very difficult for you to absorb nutrition from it, especially the books deep into Java Virtual Machine. Others may think it is better for you.
Although the source code of Tomcat is very beautiful, I do not recommend that you read it at the beginning. I will talk about this in this article and will not expand it for the moment.

I think it is the core driving force for reading source code. I have seen a vast majority of programmers and their attitudes towards learning are basically at these levels (very extreme ):
1. Pay attention to the project itself. If you don't understand it, just click Baidu.
2. In addition to doing a good job in projects, you will also read technical books related to projects and Wikipedia.
3. Besides reading project-related books, you can also read books in the IT industry. For example, when learning Java, you can also learn about function languages, such as lisp.
4. Find some open-source projects and try out a large number of third-party frameworks and write a demo.
5. Read the basic framework, J2EE specifications, and debug server kernel.

Most programs are 1st types. to 5th types require not only strong interest, but also courage: Can I understand them? Actually, you can understand it.

Patience is really important. Because you seldom read guidance articles or books on source code, and no one asks or recommends you read them. You often get stuck during reading, and a card master may be stuck in the maze. At this time, what you need to do may be a temporary interruption, and then look at it from the periphery: such as the API structure, framework design diagram.

Let me talk about how to read the Java source code and what I have learned.

Java source code
If you have been developing for about a year, you would like to use the debug function of Eclipse. Now you have the technical foundation to read the source code.
I suggest reading from the JDK source code, which is directly integrated with eclipse without any configuration.

You can start with the JDK toolkit, that is, the Java version of Data Structure and algorithm we learned, such as the list interface, arraylist, javaslist implementation, hashmap, and treemap. These data structures also involve sorting algorithms.
During the interview, the examiner will always like to ask about the difference between arraylist and vector. It takes you 10 minutes to read the source code and you will never forget it.

Then the core package, that is, string and stringbuffer.
If you have a certain Java I/O basis, read filereader and other classes. I suggest you take a look at Java in a nutshell, which contains the entire architecture of Java Io. If you do not understand the interfaces and inheritance relationships of Java I/O class libraries, reading is always confusing.
I think the Java Io package is the most elegant case for inheritance and interfaces. If you are an architect in the future, you will always deal with it, such as core development related to deployment and configuration in the project.

When reading the source code, you only need to read some core classes, such as the 20 or 30 classes similar to arraylist. For each class, you do not have to read every method. Some methods such as string have already reached the Virtual Machine layer (native method), such as the hashcode method.

Of course, if you are interested, you can take a look at the jrockit source code, the same set of APIS, two implementations, very interesting.
If you want to drill it down again, you may wish to look at the set of code for the virtual machine, such as the principle of system classloader. It is not in the JDK package, and JDK is based on it. JDK's source code zip package is only 10 MB, which seems to have 50 MB. Sun has downloaded the package, but it is very confidential. I was so excited to find and read it for myself.

Java Web development source code
Before reading The Source Code such as Tomcat, you must have accumulated some experience. My practical experience can also be said to be a better step:
1. I have written some Servlet and JSP code. Note that, instead of using struts, it is difficult to get in touch with the essence of servlet. Using struts is just a fur.
2. I have read Servlet and JSP core programming.
3. Read Sun's servlet specifications
4. Read the RFC of the HTTP protocol and debug the HTTP packets.
If you have the above foundations, I do not recommend that you start to read the Tomcat source code. I suggest you read the struts source code before reading the Tomcat source code. The struts source code is much simpler than webwork. This framework can be read by 100%, and at least I can't understand webwork by 100%. I once read the struts source code and wrote a Web framework myself.

Of course, before reading the Struts framework, you 'd better read its mailreader and other demos, which is very good.
If you have done some struts projects, you will be more comfortable reading them.
Before reading struts, it is recommended to look at the mvnforum source code, which partially implements the struts function, although this BBS is not flattering.

If you have read struts, start to read the Tomcat source code.
However, I still do not recommend that you read it directly. Read the series of articles "How Tomcat works" on the onjava website, which is the simplest version of Tomcat. It tells you how httpservletrequest is implemented inside the container, how Tomcat accepts external requests through socket, and how your servlet code is called by the Tomcat container (callback ).
To learn about JSP, you must study the Servlet Source Code Compiled by JSP by the container.
Why do I always call Tomcat a container instead of a server? Leave this question to everyone.

If you must read tomcat, read jetty. At least it is embedded. You can set breakpoint debug directly in eclispe. Although tomcat has an embedded version.

Java database source code reading
I suggest you read Sun's JDBC specifications first.
I think you must have written the JDBC code. Now you can read the source code.
If you understand the JDBC specification (Interface), you must first understand its implementation and JDBC driver. My suggestion is to read the JDBC driver of MySQL because it is open source and elegant in design. When reading the JDBC driver source code of MySQL, it is recommended to look at the inside story of MySQL. There is an official book, MySQL internals, which I read five years ago. For example, you can know how the JDBC driver of MySQL interacts with the MySQL server developed by C ++ through socket data packets (connect and query.

Through the above reading, you can know how your business code, JDBC specifications, JDBC drivers, and databases work together.
If you understand these details, you will be comfortable learning Persistence frameworks such as Hibernate and ibatis.

After reading the JDBC driver, you must read the database in the next step. A powerful database is developed using Java, HSQLDB. It is an embedded database, for example, used in Desktop client software, such as mail client.
I wrote a small article about it four years ago.

Java communication and client software
I strongly recommend Instant Messaging Software wildfire and spark. You can understand Wildfire as an MSN server, and spark as an MSN client. They communicate through the XMPP protocol.
I used to customize spark in a project. Of course, it also includes some server changes. So I have read all their source code.
I recommend them. Because:
1. XMPP is lightweight and easy to understand
2. Learn socket communication implementation, especially the C/S architecture design
3. modular design. They are all module-based. You can understand both the modular architecture and the modular technical support: the classloader application scenario of the Java Virtual Machine.
4. Event Driven Architecture. Although the GUI is event-driven, the spark design is especially elegant.

Read their source code and you will be proud to be a programmer, because their architectural design and code are so beautiful.

Java Enterprise Applications
Of course, it is frameworks such as Hibernate and spring.
Before reading the spring source code, check out the J2EE design and development written by Rod Johnson. It is the design idea of spring. Note that the Chinese version is completely ruined because it is not a Chinese version.
Before reading The Hibernate source code, you must read the hibernate in Action written by Gavin king. At the same time, you should read the enterprise application architecture model written by Martin Fowler. it specifically talks about the design idea of the persistence framework. When you have to read these two books thoroughly, let's look at their source code.
In addition, before reading the source code, you will find that they use many third-party jar packages, 20 or 30. You 'd better understand those jar packages first.

When it comes to enterprise applications, workflows are involved. I have read the source code of jbpm, and I have an article on jbpm kernel (silver fox) on the Internet ). I feel like its kernel is two thousand lines. Don't be afraid. I once read the jbpm source code blog.
Of course, to read the source code of a workflow, you must have a deep understanding of its theoretical model and have written some demos or projects.

As mentioned above, I have read it myself and it is also suitable for ordinary people to read it.
I have also read some non-Java source code, which is nice and recommended to you:
Dojo source codeIts architecture is elegantly designed, similar to Java import and extends. However, the actual application is messy. We developed our own framework based on this, but I am not the main force.

Flex source codeI used flex for a medium-sized project just after it was open-source at the end of flex 08. It should be said that it is a pioneer in domestic technology. At that time, there were no in-depth books or open-source projects in the market. I simply read the flex help documentation and source code to get the project done. Two or three years later, I think the system design is very elegant.

Now, let's introduce it here.
I have read these Java source code four years ago or even earlier. Technology changes so fast, like the rapid development of the Internet, it has given rise to many high-performance and distributed databases, such as hadoop. I saw that I was out of date.
Over the past few years, many excellent frameworks have emerged, so you may wish to share them with us.

Reprint address: http://www.baidu.com/link? Url = jG6WGJqjJ4zBBpC8yDF8xDh8vibi0ExoHW2BcokQ2cS5OEV6FjN-r1ooQ8G source code reading, I think the core has three points: technical foundation + strong thirst for knowledge + patience.

Speaking of the technical foundation, let me give you an example. If you have never learned Java or any programming language such as C ++, you should start with core Java. it is very difficult for you to absorb nutrition from it, especially the books deep into Java Virtual Machine. Others may think it is better for you.
Although the source code of Tomcat is very beautiful, I do not recommend that you read it at the beginning. I will talk about this in this article and will not expand it for the moment.

I think it is the core driving force for reading source code. I have seen a vast majority of programmers and their attitudes towards learning are basically at these levels (very extreme ):
1. Pay attention to the project itself. If you don't understand it, just click Baidu.
2. In addition to doing a good job in projects, you will also read technical books related to projects and Wikipedia.
3. Besides reading project-related books, you can also read books in the IT industry. For example, when learning Java, you can also learn about function languages, such as lisp.
4. Find some open-source projects and try out a large number of third-party frameworks and write a demo.
5. Read the basic framework, J2EE specifications, and debug server kernel.

Most programs are 1st types. to 5th types require not only strong interest, but also courage: Can I understand them? Actually, you can understand it.

Patience is really important. Because you seldom read guidance articles or books on source code, and no one asks or recommends you read them. You often get stuck during reading, and a card master may be stuck in the maze. At this time, what you need to do may be a temporary interruption, and then look at it from the periphery: such as the API structure, framework design diagram.

Let me talk about how to read the Java source code and what I have learned.

Java source code
If you have been developing for about a year, you would like to use the debug function of Eclipse. Now you have the technical foundation to read the source code.
I suggest reading from the JDK source code, which is directly integrated with eclipse without any configuration.

You can start with the JDK toolkit, that is, the Java version of Data Structure and algorithm we learned, such as the list interface, arraylist, javaslist implementation, hashmap, and treemap. These data structures also involve sorting algorithms.
During the interview, the examiner will always like to ask about the difference between arraylist and vector. It takes you 10 minutes to read the source code and you will never forget it.

Then the core package, that is, string and stringbuffer.
If you have a certain Java I/O basis, read filereader and other classes. I suggest you take a look at Java in a nutshell, which contains the entire architecture of Java Io. If you do not understand the interfaces and inheritance relationships of Java I/O class libraries, reading is always confusing.
I think the Java Io package is the most elegant case for inheritance and interfaces. If you are an architect in the future, you will always deal with it, such as core development related to deployment and configuration in the project.

When reading the source code, you only need to read some core classes, such as the 20 or 30 classes similar to arraylist. For each class, you do not have to read every method. Some methods such as string have already reached the Virtual Machine layer (native method), such as the hashcode method.

Of course, if you are interested, you can take a look at the jrockit source code, the same set of APIS, two implementations, very interesting.
If you want to drill it down again, you may wish to look at the set of code for the virtual machine, such as the principle of system classloader. It is not in the JDK package, and JDK is based on it. JDK's source code zip package is only 10 MB, which seems to have 50 MB. Sun has downloaded the package, but it is very confidential. I was so excited to find and read it for myself.

Java Web development source code
Before reading The Source Code such as Tomcat, you must have accumulated some experience. My practical experience can also be said to be a better step:
1. I have written some Servlet and JSP code. Note that, instead of using struts, it is difficult to get in touch with the essence of servlet. Using struts is just a fur.
2. I have read Servlet and JSP core programming.
3. Read Sun's servlet specifications
4. Read the RFC of the HTTP protocol and debug the HTTP packets.
If you have the above foundations, I do not recommend that you start to read the Tomcat source code. I suggest you read the struts source code before reading the Tomcat source code. The struts source code is much simpler than webwork. This framework can be read by 100%, and at least I can't understand webwork by 100%. I once read the struts source code and wrote a Web framework myself.

Of course, before reading the Struts framework, you 'd better read its mailreader and other demos, which is very good.
If you have done some struts projects, you will be more comfortable reading them.
Before reading struts, it is recommended to look at the mvnforum source code, which partially implements the struts function, although this BBS is not flattering.

If you have read struts, start to read the Tomcat source code.
However, I still do not recommend that you read it directly. Read the series of articles "How Tomcat works" on the onjava website, which is the simplest version of Tomcat. It tells you how httpservletrequest is implemented inside the container, how Tomcat accepts external requests through socket, and how your servlet code is called by the Tomcat container (callback ).
To learn about JSP, you must study the Servlet Source Code Compiled by JSP by the container.
Why do I always call Tomcat a container instead of a server? Leave this question to everyone.

If you must read tomcat, read jetty. At least it is embedded. You can set breakpoint debug directly in eclispe. Although tomcat has an embedded version.

Java database source code reading
I suggest you read Sun's JDBC specifications first.
I think you must have written the JDBC code. Now you can read the source code.
If you understand the JDBC specification (Interface), you must first understand its implementation and JDBC driver. My suggestion is to read the JDBC driver of MySQL because it is open source and elegant in design. When reading the JDBC driver source code of MySQL, it is recommended to look at the inside story of MySQL. There is an official book, MySQL internals, which I read five years ago. For example, you can know how the JDBC driver of MySQL interacts with the MySQL server developed by C ++ through socket data packets (connect and query.

Through the above reading, you can know how your business code, JDBC specifications, JDBC drivers, and databases work together.
If you understand these details, you will be comfortable learning Persistence frameworks such as Hibernate and ibatis.

After reading the JDBC driver, you must read the database in the next step. A powerful database is developed using Java, HSQLDB. It is an embedded database, for example, used in Desktop client software, such as mail client.
I wrote a small article about it four years ago.

Java communication and client software
I strongly recommend Instant Messaging Software wildfire and spark. You can understand Wildfire as an MSN server, and spark as an MSN client. They communicate through the XMPP protocol.
I used to customize spark in a project. Of course, it also includes some server changes. So I have read all their source code.
I recommend them. Because:
1. XMPP is lightweight and easy to understand
2. Learn socket communication implementation, especially the C/S architecture design
3. modular design. They are all module-based. You can understand both the modular architecture and the modular technical support: the classloader application scenario of the Java Virtual Machine.
4. Event Driven Architecture. Although the GUI is event-driven, the spark design is especially elegant.

Read their source code and you will be proud to be a programmer, because their architectural design and code are so beautiful.

Java Enterprise Applications
Of course, it is frameworks such as Hibernate and spring.
Before reading the spring source code, check out the J2EE design and development written by Rod Johnson. It is the design idea of spring. Note that the Chinese version is completely ruined because it is not a Chinese version.
Before reading The Hibernate source code, you must read the hibernate in Action written by Gavin king. At the same time, you should read the enterprise application architecture model written by Martin Fowler. it specifically talks about the design idea of the persistence framework. When you have to read these two books thoroughly, let's look at their source code.
In addition, before reading the source code, you will find that they use many third-party jar packages, 20 or 30. You 'd better understand those jar packages first.

When it comes to enterprise applications, workflows are involved. I have read the source code of jbpm, and I have an article on jbpm kernel (silver fox) on the Internet ). I feel like its kernel is two thousand lines. Don't be afraid. I once read the jbpm source code blog.
Of course, to read the source code of a workflow, you must have a deep understanding of its theoretical model and have written some demos or projects.

As mentioned above, I have read it myself and it is also suitable for ordinary people to read it.
I have also read some non-Java source code, which is nice and recommended to you:
Dojo source codeIts architecture is elegantly designed, similar to Java import and extends. However, the actual application is messy. We developed our own framework based on this, but I am not the main force.

Flex source codeI used flex for a medium-sized project just after it was open-source at the end of flex 08. It should be said that it is a pioneer in domestic technology. At that time, there were no in-depth books or open-source projects in the market. I simply read the flex help documentation and source code to get the project done. Two or three years later, I think the system design is very elegant.

Now, let's introduce it here.
I have read these Java source code four years ago or even earlier. Technology changes so fast, like the rapid development of the Internet, it has given rise to many high-performance and distributed databases, such as hadoop. I saw that I was out of date.
Over the past few years, many excellent frameworks have emerged, so you may wish to share them with us.

Reprint address: http://www.baidu.com/link? Url = jG6WGJqjJ4zBBpC8yDF8xDh8vibi0ExoHW2BcokQ2cS5OEV6FjN-r1ooQ8G

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.