Java Design Pattern---Single state mode understanding __java design pattern

Source: Internet
Author: User
Tags visibility volatile
Preface

What is a single state mode.
In a system, the JVM can only exist an instance of an object, all procedures can only call this object to complete the relevant logic operations, the single state mode is somewhat reduced by the new keyword calls, instance initialization, saving the system memory overhead. The difficulty of the single state mode is that multithreading processing, writing a lot of, really able to execute, consider, the difficulty lies in the thread security problem ... , this article carries on the simple understanding elaboration, if does not understand, please pay attention to the blogger to leave two Good article two Good article

Http://www.blogjava.net/kenzhh/archive/2013/03/15/357824.html
the correct realization of the Http://www.cnblogs.com/dolphin0520/p/3920373.html single State mode

The public class Truth {
    //volatitle guarantees the visibility of the modifier variable in memory, and with the Java memory model
    //volatile keyword can prevent order reordering, so volatile can guarantee the order in some degree
    private volatile static Truth instance = null;
    Private Truth () {} public
    static Truth getinstance () {
        if (instance = = null) {
            //guaranteed atomic operation
            synchronized ( Truth.class) {
                if (instance = = null) {
                    instance = new Truth ()
                ;
        }}} return instance;
    }

    public void Say () {
        System.out.println (' Only one truth in the world! ');
    }
Brief Description

1-Private variable instance can only be initialized internally.
2-When the instance is empty, a variable assignment is made to ensure that the returned object is not empty and multi-threaded related

1-double check [volatile|synchronized] ensures thread safety for this class
2-problems that may arise for, when two threads enter the GetInstance method at the same time, thread 1 is judged but not assigned, and thread 2 executes and assigns a value while two threads do not synchronize the public variables, then the thread 1 returns to the second instance directly when it is executed. Of course, this is when the double check has no constraints. synchronized keyword simple description

Guaranteed atomic operation, the keyword modified in the code, in a time only one thread to execute, when the execution is completed, the thread will update the latest values into memory before releasing the second thread into the code block to execute volatile keyword simple elaboration

Ensures the order and visibility of the thread, variables decorated by this keyword will be visible in all thread operations, that is, after loading execution from the CPU cache, the resulting changes will be notified to the other CPUs ' caches, which will be flagged in the cache of these CPUs, This causes the cache to be loaded from memory when other threads execute to the variable. Visibility of

What is visibility, as shown in the figure, when multithreading, the value of variables and related programs loaded into the cache, processed by the CPU, when the program on the value in memory to change processing, before flushing to the cache, the other threads to read and manipulate the processing, This will cause the value of the read to be not up to date, causing thread-safety issues, which is the visibility of the thread
Order of

The order is understood as the sequence of program execution, and in coding, all of our programs are executed sequentially, and in the JVM, including the later actual execution, the order in which we execute the program is disrupted and the execution efficiency is increased, and in single-threaded processing we do not need to consider sequential execution, but in multiple threads, It is often due to the order of execution of the program that causes thread insecurity, such as the absence of program dependencies between programs, but there are logical dependencies, which can cause thread safety problems. of Atomic

Atomic processing, that is either not to deal with, or all the program processing completed, the whole program as a whole, similar to the transaction of the database, but the program does not happen rollback, atomicity is a necessary part of the multithreading process.

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.