From: http://www.ibm.com/developerworks/cn/java/j-jtp06197.html
Introduction:The Java language includes two internal synchronization mechanisms: Synchronous block (or method) and volatile variables. Both mechanisms are proposed to achieveCodeThread security. Among them, the synchronization of volatile variables is po
One, have to mention the volatileVolatile is a very old keyword, almost with the birth of the JDK, we all know the keyword, but it is not clear when it will be used, we are ubiquitous in the JDK and open source framework, but the concurrency experts often advise us to stay away from it. such as thread, a very basic class, which is very important thread state field, is modified with volatile, see Code/* Java
Previous blog "Dead java Concurrency"-in-depth analysis of the volatile implementation principle has described the characteristics of volatile:
volatile visibility; For a volatile read, you can always see the final write of this variable;
the lock operation can be repeated multiple times by the same thread, and the variable will be unlocked only after performing the same number of unlock operations, after performing the lock multiple times.
If you perform a lock operation on a variable, that will empty the value of this variable in the working memory, and you need to re-execute the value of the load or assign manipulation initialization variable before the execution engine uses the variable.
If a variable is not locked b
Volatile keywords in JavaLearning Imageloader source encountered such a keyword encountered, did not encounter, and do not understand what the meaning of the Internet to find some information, summary record.In Java thread concurrency processing, there is a very big confusion about the use of a keyword volatile, for the use of this keyword, in the multi-threaded
1. Analysis Overview
Related Concepts of memory models
Three concepts in concurrent programming
Java memory model
Deep analysis of volatile keywords
Scenarios using the volatile keyword
2. Related concepts of memory modelCache consistency issues. It is commonly said that the variable accessed by multiple threads is a shared variable.
Java concurrent programming: the usage and principle of volatile, javavolatile
Java concurrent programming series [unfinished ]:
Java concurrent programming: core Theory
Java concurrent programming: Synchronized and its implementation principle
. After Java5, JMM's semantics of volatile were enhanced. Is the law of ③volatile variables we see.How do you understand the "atomized operation"? Look at the following example:privatestaticvolatileint0;publicstaticintgenerateSerialNumber(){ return nextSerialNum++;}The above code uses volatile to modify the nextserialnum, according to the law of the third
atomic classes, such as Atomicinteger, Atomicboolean, and so on.2. VisibilityFor visibility, Java provides the volatile keyword to guarantee visibility.When a shared variable is modified by volatile, it guarantees that the modified value is immediately updated to main memory, and when other threads need to read it, it will read the new value in RAM.The common sh
Java multithreading (3) volatile domainPreface
Sometimes synchronization is used only to read and write one or two instance domains, and the overhead is too large. The volatile keyword provides a lock-free mechanism for Synchronous access to the instance domain. If you declare a domain as volatile, the compiler and vir
Objective
Sometimes it is too expensive to use synchronization just to read or write one or two instance domains, and the volatile keyword provides a lock-free mechanism for synchronous access to instance domains. If you declare a domain to be volatile, the compiler and the virtual machine will know that the domain is likely to be updated concurrently by another thread. Before we get to the
that can be written to a volatile variable are independent of the state of any program, including the current state of the variable.1. Status Mark Amountvolatile Boolean false while (! Flag) { publicvoid Setflag () { true;}volatile Boolean false ; // Thread 1:context = loadcontext (); true ; // Thread 2: while (! inited) {Sleep ()}dosomethingwithconfig (context);2.double Checkclass
Java Multithreading-----------------volatile memory semantics The volatile keyword is the most lightweight synchronization mechanism provided by a Java virtual machine. Because the volatile keyword is related to the Java memory m
Preface
Landlord this title actually has a kind of death flavor, why, these three things can be divided into three articles to write, but, the landlord that these three things are highly relevant, should be in a knowledge point. Understand these things in a single study. To better understand the Java memory model and the volatile keyword and the HB principle.
The landlord is trying to tell the three questio
Java multi-thread volatile explanation?? Recently has been looking at some of the multi-threading knowledge, read some books and some blog, harvest or a lot of, recently read the "Java Concurrent programming Art" This book feels great Harvest is also recommended to you, but also combined with the blog before the good summary of what you learn, there are insuffici
We learned from the previous chapter that synchronized is a heavyweight lock, and although the JVM has done a lot of optimizations for it, the volatile described below is a lightweight synchronized. If a variable uses volatile, it is less expensive than using synchronized because it does not cause thread context switching and scheduling. The Java language Specifi
Used in multi-threading, synchronous variables. thread to improve efficiency, a member variable (such as a) copy a copy (such as b), the thread of access to a actually access is B. Synchronization of A and B occurs only in certain actions. Thus there is a and B inconsistency. Volatile is used to avoid this situation. Volatile tells the JVM that the variable it modifies does not keep the copy and accesses th
We learned from the previous chapter that synchronized is a heavyweight lock, and although the JVM has done a lot of optimizations for it, the volatile described below is a lightweight synchronized. If a variable uses volatile, it is less expensive than using synchronized because it does not cause thread context switching and scheduling. The Java language Specifi
Java Theory and Practice: Correct Use of Volatile variables (1)
The volatile variable in Java can be seen as a "to a lesser extent"synchronized"; AndsynchronizedCompared with the block, the volatile variable requires less encoding and less runtime overhead. However, the on
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.