volatile

Workings of AtomicReferenceArray

[亡魂溺海] 提交于 2019-12-10 10:33:46
问题 I am wondering if AtomicReferenceArray can be used as a replacement for ConcurrentLinkedQueue (if one could live with a bounded structure). I currently have something like: ConcurrentLinkedQueue<Object[]> queue = new ConcurrentLinkedQueue<Object[]>(); public void store(Price price, Instrument instrument, Object[] formats){ Object[] elements = {price, instrument, formats}; queue.offer( elements); } The store(..) is called by multiple threads. I also have a consumer thread, which periodically

Sequential consistency volatile explanation

落爺英雄遲暮 提交于 2019-12-10 10:09:00
问题 I am watching video from java jpoint conference. I have question about following slide from Alexey Shipilev report: Excuse me for non-english on slide. Actually author says that it is impossible that variable set will be r1 = 1 (Y) r2 = 0 (x) r3 = 1 (x) r4 = 0 (Y) According the video he implies that it is obviously. Can someone clarify why this value set impossible according JMM? P.S. If I understand Alexey notation correct it respects the following code: public class SequentialConsistency {

多线程学习(5)volatile 和 synchronized 的区别

自闭症网瘾萝莉.ら 提交于 2019-12-10 09:59:51
满足同步三个基本条件: 1.原子性 原子性是指操作是不可分的。其表现在于对于共享变量的某些操作,应该是不可分的,必须连续完成。例如a++,对于共享变量a的操作,实际上会执行三个步骤,1.读取变量a的值 2.a的值+1 3.将值赋予变量a 。 这三个操作中任何一个操作过程中,a的值被人篡改,那么都会出现我们不希望出现的结果。所以我们必须保证这是原子性的。 2.可见性:一个线程对共享变量值得修改,能够及时的被其他线程看到 多线程高并发中,线程之间的通信是靠内存共享,即一个对象或变量,它在堆中的主内存中,每一个请求可以看做一个线程,每一个线程,都想操作这个主内存的对象或变量,线程会复制一份主内存中的对象或变量,到自己的线程本地栈内存中,直到操作完毕,才会将本地栈内存的对象或变量,赋值给主内存。如果不加同步锁,主内存中的对象或变量,值就容易被最后提交操作的线程覆盖,发生错误。 3.有序性: 指令重排序:代码书写的顺序与实际执行的顺序不同,指令重排序是编译器或处理器为了提高程序性能而做的优化。 1.编译器优化的重排序(编译器优化) 2.指令级并行重排序(处理器优化) 3.内存系统的重排序(处理器优化) 是不是所有的语句的执行顺序都可以重排呢? 答案是否定的。为了讲清楚这个问题,先讲解另一个概念:数据依赖性 什么是数据依赖性? 如果两个操作访问同一个变量,且这两个操作中有一个为写操作

Why this program does not go into infinite loop in absence of volatility of a boolean condition variable?

为君一笑 提交于 2019-12-10 09:25:16
问题 I wanted to understand on when exactly I need to declare a variable as volatile. For that I wrote a small program and was expecting it to go into infinite loop because of missing volatility of a condition variable. It did not went into infinite loop and worked fine without volatile keyword. Two questions: What should I change in the below code listing - so that it absolutely requires use of volatile? Is C# compiler smart enough to treat a variable as volatile - if it sees that a variable is

多线程-volatile

孤人 提交于 2019-12-10 06:28:46
volatile关键字 1.多线程内存模型 2.做个小的测试 public class VolatileTest implements Runnable { //当为false时线程结束 private static /*volatile*/ boolean flag = true ; private static int value = 100 ; @Override public void run ( ) { // TODO Auto-generated method stub while ( flag ) { value ++ ; //System.out.println(value);//可以取消注释试一试 } System . out . println ( value ) ; } public static void main ( String [ ] args ) throws InterruptedException { new Thread ( new VolatileTest ( ) ) . start ( ) ; Thread . sleep ( 1000 ) ; new Thread ( new Runnable ( ) { @Override public void run ( ) { // TODO Auto-generated method stub

正确使用 Volatile 变量

落爺英雄遲暮 提交于 2019-12-10 02:28:30
Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized ”;与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized 的一部分。本文介绍了几种有效使用 volatile 变量的模式,并强调了几种不适合使用 volatile 变量的情形。 锁提供了两种主要特性: 互斥(mutual exclusion) 和 可见性(visibility) 。互斥即一次只允许一个线程持有某个特定的锁,因此可使用该特性实现对共享数据的协调访问协议,这样,一次就只有一个线程能够使用该共享数据。可见性要更加复杂一些,它必须确保释放锁之前对共享数据做出的更改对于随后获得该锁的另一个线程是可见的 —— 如果没有同步机制提供的这种可见性保证,线程看到的共享变量可能是修改前的值或不一致的值,这将引发许多严重问题。 Volatile 变量 Volatile 变量具有 synchronized 的可见性特性,但是不具备原子特性。这就是说线程能够自动发现 volatile 变量的最新值。Volatile 变量可用于提供线程安全,但是只能应用于非常有限的一组用例:多个变量之间或者某个变量的当前值与修改后值之间没有约束。因此,单独使用 volatile 还不足以实现计数器

Preferring synchronized to volatile

半世苍凉 提交于 2019-12-10 01:45:50
问题 I've read this answer in the end of which the following's written: Anything that you can with volatile can be done with synchronized, but not vice versa. It's not clear. JLS 8.3.1.4 defines volatile fields as follows: A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4). So, the volatile fields are about memory visibility. Also, as far as I got from the answer I cited, reading and writing to volatile

图解 java线程安全

和自甴很熟 提交于 2019-12-09 21:18:36
什么是线程 线程安全 Java内存模型 - JMM 什么是 JMM 线程安全的本质 线程同步 Synchronized 关键字 Synchronized 作用 Volatile 关键字 Volatile 作用 java.util.concurrent.atomic Lock 详情参考 作者:七彩祥云至尊宝 https://juejin.im/post/5d2c97bff265da1bc552954b 来源: CSDN 作者: 轩辕朗 链接: https://blog.csdn.net/weixin_45805060/article/details/103464148

synchronized和volatile使用

为君一笑 提交于 2019-12-09 20:28:46
synchronized和volatile volatile :保证内存可见性,但是不保证原子性; synchronized:同步锁,既能保证内存可见性,又能保证原子性; synchronized实现可重入锁 (1.持有同一锁自动获取 2.继承锁) 锁定的对象有两种:1.类的实例(对象锁) 2.类对象(类锁) 对象锁(synchronized修饰普通方法或代码块) 对象锁已被其他调用者占用,则需要等待此锁被释放 /** * 对象锁的两种方式 */ //方式一 private int count =10; public synchronized void test01() { count--; System.out.println(Thread.currentThread().getName()+"count="+count); } //方式二 public void test02() { synchronized(this) { count--; System.out.println(Thread.currentThread().getName()+"count="+count); } } 类锁(synchronized修饰静态方法) 所有类实例化对象互斥拥有一把类锁 private static int count =10; /** * 类锁两种表现方式 */ public

What is the effect of InterlockedIncrement argument declared as volatile

可紊 提交于 2019-12-09 19:02:46
问题 InterlockedIncrement and other Interlocked operations declare their arguments as volatile. Why? What is the intention and effect of this? 回答1: The probable effect is very minimal. The most likely intent is to allow users to pass volatile -qualified variables to these functions without the need for a typecast. 回答2: This is done so that the function can be called both on normal variables and on volatile variables. You cannot pass a volatile variable into a function which is not expecting a