Deep understanding of Java Virtual Machine part fifth efficient concurrency

Source: Internet
Author: User

Volatile is a Java virtual machine that provides the most lightweight synchronization mechanism.

Volatile two features: 1, ensure that the synchronized variables are visible to all threads . Although it is instantly visible to all threads, it is not guaranteed to be atomic, that is, it is not guaranteed to be thread safe, for example, to create 20 threads, each thread performs a i++ operation and executes 100 times, but the result of I output is less than 2000. Because a i++ with JAVAP is executed by 4 instructions. So we guarantee atomicity through synchronized. A volatile scenario is given below: when the Shutdown () method is executed, the DoWork () method in all threads is guaranteed to stop immediately.

1 volatile Booleanshutdownrequested;2  Public voidshutdown () {3shutdownrequested =true;4 }5  Public voidDoWork () {6      while(!shutdownrequested) {7     ``````````8     }9}

2, prohibit command reordering optimization.

Let's give a simple example

1 Public classsingleton{2   Private volatile StaticSingleton instance;3    Public StaticSingleton getinstance () {4       if(Instance = =NULL){5          synchronized(Singleton.class){6              if(instance==NULL){7Instance =NewSingleton ();8 }9          }    Ten       }   One       REturninstance; A   }         - } -  Public Static voidMain (string[] args) { the singleton.getinstance (); -}

Where the instance variable is assigned the part performs the "Lock Addl $0x0, (%ESP)" operation, which is equivalent to a memory barrier (the Memories Barrier), and the reorder instructions cannot be placed before the memory barrier.

Understanding of atomicity, visibility and orderliness:

The Java memory model is built around the three characteristics of how atomicity, visibility, and ordering are handled in a concurrency process.

Atomicity: Atomic operations include read, load, assign, use, store, and write. The operation between lock and unlock, synchronized blocks is also atomic.

Visibility: volatile, when a thread modifies the value of a shared variable, other threads can immediately get the value of the change. Synchronized before a variable performs a unlock operation, the variable must be synchronized back to main memory and finally the finally decorated field is initialized in the constructor once the initialization is complete, and the constructor does not pass the reference to this. You can see the finally field in all other threads.

Ordering: Volatile itself has the semantics of prohibiting command reordering, while synchronized is "one variable at a time that allows only one thread to perform unlock operations on it."

When we measure concurrent thread safety, we should not be disturbed by the time sequence, and everything must be based on the principle of prior occurrence.

Deep understanding of Java Virtual Machine part fifth efficient concurrency

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.