Thinking about javakeywordsynchronized--single-case model

Source: Internet
Author: User

The wonderful design pattern feast has just come to a curtain. Three days of time. Really learned a lot. , there are a lot of problems left over. Let's talk about synchronized this keyword today. The thinking about Synchronizedkeyword is triggered from a singleton pattern.

The initialization of an object is defined as a null value, using deferred loading in your code. The construction of the object is done when it is needed, getinstance this method.

This is what we often call the lazy singleton pattern.


the appearance of a lazy-type single case:


Suppose you have the advantage of putting the class initialization process into your code run. is to start fast. Assume that the object instantiation process is more complex. This will provide efficiency. Speed. Create objects when you use them. But there is a problem with the same lazy singleton pattern. For example , thread A wants to use Singletonclass, calling the GetInstance () method. Because it is the first call. A The instance is found to be null. It then starts to create the instance. At this point in time, the CPU happens to switch between slices. Thread B starts running, and it uses Singletonclass to call the GetInstance () method. The same detection to instance is null--note, which is switched after a test is finished. That is, a doesn't have time to create the object--so B starts creating. After the B is created, switch to a to continue running, as it has been tested. So a will not be tested again. It will create the object directly.

In this way, threads A and B each have a Singletonclass object-a single case failure!


synchronized Synchronous lock leads:

by introducing deferred loading to lazy-style, the topic that we are going to talk about today is finally drawn. So how to solve the above one about the creation of objects, CPU switching problem? In fact the method is very easy. is to add this getinstance method lock, is to getinstance () plus synchronous lock. One thread must wait for another thread to be created and then use this method, which guarantees the uniqueness of the Singleton.

Two concurrent threads to access a method in the same object, obj, in the synchronized adornment, only one thread can be run within a single time. That is, a operation of talent enough to let B operation, otherwise B has been in a state of waiting. Can also be said to be a state of blockage.


The corresponding code can be:

public class Singletonclass {   private static singletonclass instance = null;       Public synchronized static Singletonclass getinstance () {     if (instance = = null) {       instance = new Singletonclass ();     }     return instance;   }       Private Singletonclass () {        }     }



When do you use the synchronized?


When you write a class, assume that the code in the class may be executed in a multithreaded environment. Then you should consider the problem of synchronization. The language-level synchronization primitive--synchronized is built into Java, which greatly simplifies the use of multithreaded synchronization in Java. A lock can be used not only to instantiate a class, but also to use the declaration of a static member method.


synchronized advantages and disadvantages?

through its strengths can see its shortcomings is what, the advantage is to solve the problem of multithreading, avoid the use of resources, occupy multiple objects. The relative disadvantage is to lock a section of the program, other procedures assume that the need to invoke the words must wait, reduce the speed, efficiency. It is possible to create a deadlock that causes the program to break.


Summary:

The previous understanding of the singleton pattern was only to know that it could be used to instantiate a Window object, and that only one object was instantiated. Now learned the J2SE thread that chapter, is also dizzy. Do not understand this sync lock. When I recall this singleton pattern again, combined with the definition of synchronized, I learned a lot about the resonance with the previous study.

It works very well.


Thinking about javakeywordsynchronized--single-case model

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.