Use of high concurrency synchronous volatile in Java

Source: Internet
Author: User
Tags volatile

Introduction:

Synchronized and volatile play an important role in multithreaded concurrent programming, and volatile is a lightweight synchronizedthat guarantees the "visibility" of shared variables in multiprocessor development.

Visibility means that when a thread modifies a shared variable, another thread can read the modified value.

official definition of volatile

The definition of volatile in the third edition of the Java Language specification is as follows: The Java programming language allows threads to access shared variables, and in order to ensure that shared variables can be updated accurately and consistently, the thread should ensure that the variable is obtained separately through an exclusive lock. The Java language provides volatile, which in some cases is more convenient than a lock. If a field is declared as a Volatile,java thread, the memory model ensures that all threads see that the value of this variable is consistent.

Why use volatile?

The volatile variable modifier, if used appropriately , is less expensive to use and execute than synchronized because it does not cause thread context switching and scheduling.

use optimization for volatile

The famous Java concurrency programmer, Doug Lea, adds a queue collection class LinkedTransferQueue in the concurrency package of JDK7, where he uses a volatile variable to optimize the team's performance by appending a byte.

Additional word energy-saving optimized performance? This approach looks magical, but understanding the processor architecture will understand the mysteries. Let's take a look at the LinkedTransferQueue class, which uses an inner class type to define the queue's head queue (head) and tail node (tail). And this inner class paddedatomicreference does only one thing with respect to the parent class Atomicreference, appends the shared variable to 64 bytes. We can calculate that a reference to an object takes up 4 bytes, it appends 15 variables to a total of 60 bytes, plus the parent class's value variable, altogether 64 bytes.

Related issues:

1) Can I create a volatile array in Java?

Yes, you can create an array of volatile types in Java, but only a reference to an array, not an entire array. I mean, if you change the array that the reference points to, it will be protected by volatile, but if multiple threads change the elements of the array at the same time, the volatile identifier will not be able to act as a protection.

2) Does volatile make a non-atomic operation into an atomic operation?

A typical example is a long type member variable in a class. If you know that the member variable will be accessed by multiple threads, such as counters, prices, etc., you'd better set it to volatile. Why? Because reading a long type variable in Java is not atomic, it needs to be divided into two steps, and if one thread is modifying the value of the long variable, the other thread may see only half of that value (the first 32 bits). However, reading and writing to a volatile long or double variable is atomic.

3) What is the practice of volatile modifiers?

One practice is to use volatile to modify long and double variables so that they can be read and written by atomic type. Double and long are 64 bits wide, so for these two types of reading is divided into two parts, the first read the first 32 bits, and then read the remaining 32 bits, the process is not atomic, but the Java volatile type long or double variable read and write is atomic. Another effect of the volatile modifier is to provide memory barriers (barrier), such as applications in distributed frameworks. Simply put, it is when you write a volatile variable that the Java memory model inserts a write barrier (write barrier), and before reading a volatile variable, it inserts a reading barrier (read barrier). This means that when you write a volatile domain, you can ensure that any thread can see the value you write, and that the update of any value is visible to all threads before writing, because the memory barrier updates all other write values to the cache.

4) What are the guarantees for volatile type variables?

Volatile variables provide order and visibility guarantees, for example, the JVM or JIT reordering statements for better performance, but the volatile type variable is not reordered with other statements even if there are no synchronized blocks. Volatile provides happens-before guarantees that a thread's modifications can be visible to other threads. In some cases, volatile can also provide atomicity, such as reading 64-bit data types, such as long and double are not atomic, but the volatile type of double and long is atomic.

Use of high concurrency synchronous volatile in Java

Related Article

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.