Java lazy-initially solving security problems, java lazy

Source: Internet
Author: User

Java lazy-initially solving security problems, java lazy

00:10:14

Lazy style:

Class text
{Public String k;
Private static text t = null; // The structure of the Code on the right is more complex than that on the top, which may cause security problems.

Privatetest () {} // security issues undoubtedly violate this program. The original intention is to create multiple instances.

Public static text getObject ()

{// When multithreading occurs, the running status is in the ready state (simply put, the thread loses the CPU execution right)
If (t = null)
{// ----------> To the ready zone Tread1, Tread2 goes down in sequence (the thread loses the CPU execution right)
T = new text (); // the preceding two threads have created two instances.
Return t; // when there are too many processes at the cup load, and the thread is the execution unit of the process, the above situation may occur during switching.
}
Return t;
}
}

// How to solve the above problems

The Code is as follows:

Class test
{Public String k;
Private static text t = null;

Privatetest (){}

Public static text getObject ()

{
If (t = null)

//A new judgment is added to determine that once an object is generated, the lock will not be judged.

//To optimize the code (save time)

{Synchronized (test. class) {// There is a lock here. Only one thread can be entered, so no multiple threads will be used to create multiple instances.

If (t = null ){

T = new text (); // Add: Because the method is static, this cannot be called,

Return t; // The generated class name. class bytecode object is called;

//Add test. class in more detail below
                      

}

}

}
Return t;
}
    

 

--------------------------------------------------------- I am a separator ---------------------------------------------------

 

Supplement:

Singleton mode:

  Hungry Chinese style: 

/* Hungry */
/* Class test
{Public String k;
Private static final text t = new text ();// Write final as an unchangeable constant in regular writing
Private text (){}
Public static text getObject (){
Return t;// An object is generated when the class is loaded. The code structure is simple and there is basically no security problem.

// It takes longer time to load a class than the lazy class, and more time to call an object than the lazy class.
}

One of the methods to obtain bytecode objects:

Class Name. class, you can understand that the bytecode itself is static. When the class is loaded, the bytecode enters the JVM. So class. class is like class calling static methods. It is like calling bytecode objects.

  
------------------------------------ Description ----------------------------------------------------------------------------------------------------------------

I am not a freshman, but I wrote my learning insights for the first time. If there is a problem with the program or the description is unclear, please leave a message. Thank you.

If you have a better way to learn from each other and make progress together, ljs.

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.