volatile

volatile 说明

一个人想着一个人 提交于 2020-01-04 02:51:39
volatile这个关键字可能很多朋友都听说过,或许也都用过。在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果。在Java 5之后,volatile关键字才得以重获生机。volatile关键字虽然从字面上理解起来比较简单,但是要用好不是一件容易的事情。由于volatile关键字是与Java的内存模型有关的,因此在讲述volatile关键之前,我们先来了解一下与内存模型相关的概念和知识,然后分析了volatile关键字的实现原理,最后给出了几个使用volatile关键字的场景。以下是本文的目录大纲:一.内存模型的相关概念二.并发编程中的三个概念三.Java内存模型四..深入剖析volatile关键字五.使用volatile关键字的场景若有不正之处请多多谅解,并欢迎批评指正。请尊重作者劳动成果,转载请标明原文链接:http://www.cnblogs.com/dolphin0520/p/3920373.html一.内存模型的相关概念大家都知道,计算机在执行程序时,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入。由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这时就存在一个问题,由于CPU执行速度很快,而从内存读取数据和向内存写入数据的过程跟CPU执行指令的速度比起来要慢的多

Volatile 说明

元气小坏坏 提交于 2020-01-04 02:51:08
Java™ 语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量。这两种机制的提出都是为了实现代码线程的安全性。其中 Volatile 变量的同步性较差(但有时它更简单并且开销更低),而且其使用也更容易出错。 Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized ”;与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized 的一部分。本文介绍了几种有效使用 volatile 变量的模式,并强调了几种不适合使用 volatile 变量的情形。 锁提供了两种主要特性: 互斥(mutual exclusion) 和 可见性(visibility) 。互斥即一次只允许一个线程持有某个特定的锁,因此可使用该特性实现对共享数据的协调访问协议,这样,一次就只有一个线程能够使用该共享数据。可见性要更加复杂一些,它必须确保释放锁之前对共享数据做出的更改对于随后获得该锁的另一个线程是可见的 —— 如果没有同步机制提供的这种可见性保证,线程看到的共享变量可能是修改前的值或不一致的值,这将引发许多严重问题。 Volatile 变量 Volatile 变量具有 synchronized 的可见性特性,但是不具备原子特性。这就是说线程能够自动发现

Why is it dangerous to get rid of volatile?

人走茶凉 提交于 2020-01-03 10:57:10
问题 In C++, volatile is treated the same way const is: passing a pointer to volatile data to a function that doesn't want the volatile modifier triggers a compile error. int foo(int* bar) { /* snip */ } int main() { volatile int* baz; foo(baz); // error: invalid conversion from ‘volatile int*’ to ‘int*’ } Why is it dangerous? It's obvious for the const modifier that removing it can break const correctness; but is there such a thing as " volatile correctness"? I can't figure out how passing a

Should infinite loop condition variables always be declared as volatile?

ぃ、小莉子 提交于 2020-01-03 04:48:26
问题 Consider, such type of code, while( !cond ) ; If cond is not declared volatile, the compiler can optimize it by caching it in a register. In that case, the while loop will continue even after cond is set. Now does that mean that any such kind of variable should always be declared volatile ? Why aren't the compilers smart enough to realize that it should not cache such variables? 回答1: Why would it not cache the variable? You don't change it in the loop, so it's equivalent, in C's mind, to

Can unsafe type punning be fixed by marking a variable volatile?

泪湿孤枕 提交于 2020-01-02 05:28:11
问题 In zwol's answer to Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member? he gives an example of why a simple typecast between similar structs isn't safe, and in the comments there is a sample environment in which it behaves unexpectedly: compiling the following with gcc on -O2 causes it to print "x=1.000000 some=2.000000" #include <stddef.h> #include <stdio.h> #include <stdlib.h> struct base { double some; char

Android Studio 2.0 update - public static volatile com.android.tools.fd.runtime.IncrementalChange

眉间皱痕 提交于 2020-01-02 03:35:33
问题 After I update with Android 2.0 a new field has been added into my models object public static volatile com.android.tools.fd.runtime.IncrementalChange com.pr4.models.User.$change What problem I have faced : i.e I was reading the field using reflection package and creating a dynamic table using fields, and my app get crashed due to new field $change, If i put a check to skip particular modifier field then I have to make lot of changes. Why a new field was added at runtime in my code, how to

What is the difference between sequential consistency and atomicity?

血红的双手。 提交于 2020-01-02 02:11:11
问题 I read that java volatile are sequential consistent but not atomic. For atomicity java provides different library. Can someone explain difference between two, in simple english ? (I believe the question scope includes C/C++ and hence adding those language tags to get bigger audience.) 回答1: Imagine those two variables in a class: int i = 0; volatile int v = 0; And those two methods void write() { i = 5; v = 2; } void read() { if (v == 2) { System.out.println(i); } } The volatile semantics

understanding of Volatile.Read/Write

╄→尐↘猪︶ㄣ 提交于 2020-01-02 00:51:10
问题 I'm trying to understand the C# Volatile class. As i read: The Volatile.Write method forces the value in location to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to Volatile.Write. The Volatile.Read method forces the value in location to be read from at the point of the call. In addition, any later program-order loads and stores must occur after the call to Volatile.Read. Is that means the in the case of: internal

多线程学习:Volatile与Synchronized的区别、什么是重排序

大城市里の小女人 提交于 2020-01-01 17:37:25
java线程的内存模型   java的线程内存模型中定义了每个线程都有一份自己的共享变量副本(本地内存),里面存放自己私有的数据,其他线程不能直接访问,而一些共享变量则存在主内存中,供所有线程访问。 上图中,如果线程A和线程B要进行通信,就要经过主内存,比如线程B要获取线程A修改后的共享变量的值,要经过下面两步: (1)、线程A修改自己的共享变量副本,并刷新到了主内存中。 (2)、线程B读取主内存中被A更新过的共享变量的值,同步到自己的共享变量副本中。 总结:在java内存模型中,共享变量存放在主内存中,每个线程都有自己的本地内存,当多个线程同时访问一个数据的时候,可能本地内存没有及时刷新到主内存,所以就会发生线程安全问题。 java多线程中的三个特性:   原子性: 即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行。一个很经典的例子就是银行账户转账问题:比如从账户A向账户B转1000元,那么必然包括2个操作:从账户A减去1000元,往账户B加上1000元。这2个操作必须要具备原子性才能保证不出现一些意外的问题。   可见性: 当多个线程访问同一个变量时,一个线程修改了这个变量的值,其他线程能够立即看得到修改的值。   有序性: 就是 程序执行的顺序按照代码的先后顺序执行。 一般来说处理器为了提高程序运行效率,可能会对输入代码进行优化

从三个特性理解多线程开发

岁酱吖の 提交于 2020-01-01 17:37:10
工作中许多地方需要涉及到多线程的设计与开发,java多线程开发当中我们为了线程安全所做的任何操作其实都是围绕多线程的三个特性:原子性、可见性、有序性展开的。针对这三个特性的资料网上已经很多了,在这里我希望在站在便于理解的角度,用相对直观的方式阐述这三大特性,以及为什么要实现和满足三大特性。 一、原子性 原子性是指一个操作或者一系列操作要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行。其实这句话就是在告诉你,如果有多个线程执行相同一段代码时,而你又能够预见到这多个线程相互之间会影响对方的执行结果,那么这段代码是不满足原子性的。结合到实际开发当中,如果代码中出现这种情况,大概率是你操作了共享变量。 针对这个情况网上有个很经典的例子,银行转账问题: 比如A和B同时向C转账10万元。如果转账操作不具有原子性,A在向C转账时,读取了C的余额为20万,然后加上转账的10万,计算出此时应该有30万,但还未来及将30万写回C的账户,此时B的转账请求过来了,B发现C的余额为20万,然后将其加10万并写回。然后A的转账操作继续——将30万写回C的余额。这种情况下C的最终余额为30万,而非预期的40万。 如果A和B两个转账操作是在不同的线程中执行,而C的账户就是你要操作的共享变量,那么不保证执行操作原子性的后果是十分严重的。 OK,上面的状况我们理清楚了,由此可以引申出下列三个问题 1