Deep understanding of how proxy mode optimizes system performance in Java

Source: Internet
Author: User

Recently participating in the Internship project iteration, the project needs to achieve a rapid response to the system and a large number of data processing. In the continuous learning to get a bit of experience, first recorded. Change slowly!
Knowledge of the agent model and simple application before the study notes have been recorded, you can review. The main record here is how to use the proxy mode to implement lazy loading, which can improve system performance and response speed.

A simple example of lazy loading using proxy mode:
requirements : In the project to the system start speed made certain requirements

When we first load the system, because we do not need the actual data to construct the display interface, this provides the possibility for us to realize the fast response of the first load of the system. In general mode, we typically start initializing all classes at system startup, and if many classes in the system are designed to get a database connection (such as initializing a connection pool) or similar operations (such as XML parsing), all of these operations are stacked together, slowing down the system's startup speed. At this point we can consider the use of proxy mode, using proxy classes, encapsulating some initialization operations (such as database connection pool building), take the database as an example: Use the proxy class to encapsulate the initialization of the database query, when the system starts, initialize the proxy class, rather than the real database query class. The agent class has nothing to do, so the speed of the construction gets improved.
Keep this in mind: Use the proxy mode to separate the most resource-consuming methods (if possible), and wait until the user is actually using the proxy class to load the real class separately, completing the user's request, because not all real classes are likely to be used by the user.
The core idea of lazy loading: If this component is not currently being used, you do not need to actually initialize it, use a proxy object instead of his original location, and load it until it is really needed.
The implementation of the proxy mode:

 Public  class realsubject implements Subject{     Public Realsubject(){Try{Thread.Sleep ( +);//Analog system start-up time consumption}Catch(Exception e)        {E.printstacktrace (); }    } Public void dosomething(){// ...}}The //proxy class is a lightweight object that is created quickly PublicCALSS Subjectproxy implements subject{PrivateRealsubject real =NULL; Public void dosomething(){//Real objects are created when the component is actually used        if(Real = =NULL) Real =NewSunbject ();returnReal.dosomething (); }}

Deep understanding of how proxy mode optimizes system performance in Java

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.