Multi-threaded Learning Note two

Source: Internet
Author: User

Single-case Design patterns and Multithreading:
Design pattern: An effective solution to the problem. is actually a kind of thought.
Single Case design mode:
Problem solved: The object uniqueness of a class can be guaranteed in memory.
For example, when you use the same configuration information object for multiple programs, you need to guarantee the uniqueness of the object.
What if uniqueness is guaranteed?
1. No other program is allowed to create the class object with new.
2. Create an instance of this class in this class.
3. Provide a way for other programs to get objects.
Implementation steps:
1. Privatize the constructor for this class.
2. Create a class object in this class by using new.
3. Define a common method to return the created object.

class single{        Private single () {}                new single ();                  Public void getinstance () {            return  s;        }    }    

How do we call this object next? By privatizing the constructor of the single class, we have already organized our new class, so we are going to get the object of the class by the class name. getinstance ();
So the method must be static, because the method is going to return s, so S is also static.

class single{        Private single  () {}                staticnew single ();                  Public Static void getinstance () {            return  s;        }    }

Because S is a member variable, we can get the object of that class by the way of the class name, which is the form of SINGLE.S.
Controllability can be achieved through access to the method. For example, I can pass a parameter in the method, as long as the passed parameter satisfies a condition to return s, which is called controllability.
S is a member variable of the single class, we generally do not expose the member variable, because it is controllable.

class single{        Private single  () {}                privatestaticnew  Single ();                  Public Static void getinstance () {            return  s;        }    }

The above is the basic idea of the singleton model. Simply by adding the above code to any of the classes, you can guarantee the uniqueness of the class object.
This method class creates an object as soon as it is loaded. The other is described below:

 class   single{ private   single () {}  private static  single s = null  ;  public  static  void   getinstance () { if  (s = = Span style= "color: #0000ff;" >null  ) {s  = new   single ();             return   S; }        }    }

The above form is the object that is created when the method is called. Also known as deferred load form.
In order to distinguish between two forms: the first is called a Hungry man, and the second is the lazy type. The development uses the A Hungry man type, the interview uses the lazy type, because the lazy type contains the knowledge point to be more.
Lazy-type in multi-threading will have a security problem.
Why not a hungry man?
Multi-threaded security issues occur because there are multiple threads that have common access to a shared resource, and one reason is that there are multiple lines of code that perform operations to share data.
And the A Hungry man type has only one-->return s; so there is no security problem.
Solution a hungry man type:

classsingle{PrivateSingle () {}Private StaticSingle S =NULL;  Public Static synchronized voidgetinstance () {if(s = =NULL) {s=NewSingle (); returns; }        }    }

However, the above solution will judge the lock every time the method is called, which reduces the efficiency. To improve efficiency we rewrite the code without synchronizing the function lock, instead of synchronizing the block lock.

classsingle{PrivateSingle () {}Private StaticSingle S =NULL;  Public Static voidgetinstance () {if(s = =NULL){                synchronized(Single.class){//Note: This method cannot be used for static methods.                    if(s = =NULL) {s=NewSingle (); returns; }                }            }        }    }

Explanation: A synchronous block lock is used to solve thread safety problems, and one more time to judge is to improve efficiency.
To sum up: the development or use of a hungry man-style good.

Multi-threaded Learning Note two

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.