volatile

Volatile解析

↘锁芯ラ 提交于 2019-12-18 02:27:22
Volatile解析 Volatile是jvm提供的轻量级的同步机制,具有三大特性: 保证可见性 不保证原子性 禁止指令重排 1.JMM及可见性 想要了解volatile的三大特性,首先需要了解JMM(Java Memory Model,Java内存模型)。JMM本身是一种抽象的存在,它描述了一种规范,规定了程序中各种变量的访问方式。JMM关于同步的规定包括下面三部分: 线程解锁前,必须把共享变量的值刷新回主内存 线程加锁前,必须读取主内存中最新值到自己的工作内存 加锁解锁是同一把锁 何为主内存、工作内存? 在硬件层面,计算机的存储主要分为以下几:硬盘、内存、缓存(cache),其中缓存是用于内存与cpu交流的桥梁,因为cpu的运算速度远高于内存的读写,cpu不能总是等待数据从内存中读取,所以引入缓存,将cpu需要的数据先从内存读取到缓存中,cpu使用时直接从缓存中获取,数据处理完之后放回到缓存中,再将缓存中最新的数据同步到内存中。 主内存和工作内存的概念跟内存和缓存的概念十分相似,主内存就相当于内存,多线程共用一份,工作内存就相当于缓存,数据是从主内存中拷贝的副本。java程序的运行是承载在线程上的,每个线程都有自己的一份私有数据空间,也就是工作内存,而JMM规定所有变量的值都存储在主内存中,供所有线程共享使用,但是线程对变量的操作必须在自己的工作内存中进行,因此线程在操作变量前

The code example which can prove “volatile” declare should be used

给你一囗甜甜゛ 提交于 2019-12-18 02:20:14
问题 Currently I can't understand when we should use volatile to declare variable. I have do some study and searched some materials about it for a long time and know that when a field is declared volatile, the compiler and runtime are put on notice that this variable is shared and that operations on it should not be reordered with other memory operations. However, I still can't understand in what scenario we should use it. I mean can someone provide any example code which can prove that using

How does volatile actually work?

孤街醉人 提交于 2019-12-17 23:37:45
问题 Marking a variable as volatile in Java ensures that every thread sees the value that was last written to it instead of some stale value. I was wondering how this is actually achieved. Does the JVM emit special instructions that flush the CPU cashes or something? 回答1: From what I understand it always appears as if the cache has been flushed after write, and always appears as if reads are conducted straight from memory on read. The effect is that a Thread will always see the results of writes

java并发编程实战wwj----------------------第一阶段--------------35-36-37-38-39

我的梦境 提交于 2019-12-17 23:18:27
线程是非常重的: 线程池为什么要有它: 线程创建要开辟虚拟机栈,释放线程要垃圾回收的。 server端要并发访问数据库的。 服务器启动有线程池放着。 ----- 线程池的概念: 1.任务队列 2.拒绝策略(抛出异常,直接丢弃,阻塞,放在临时队列) 3.初始化值init(min),初始线程池大小 3.active 5.max线程池线程的最大个数 min<=active<=max quartz:定时任务 -------------------------------------------------------------------------------35-------------------------------------------------------------------------------------- 代码: package chapter13; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class SimpleThreadPoolMy { private final int size; private final static int DEFAULT_SIZE = 10; private static volatile

Java volatile variable doesn't behave correctly.

给你一囗甜甜゛ 提交于 2019-12-17 21:02:36
问题 public class MyThread { volatile static int i; public static class myT extends Thread { public void run () { int j = 0; while(j<1000000){ i++; j++; } } } public static void main (String[] argv) throws InterruptedException{ i = 0; Thread my1 = new myT(); Thread my2 = new myT(); my1.start(); my2.start(); my1.join(); my2.join(); System.out.println("i = "+i); } } Since volatile builds happens-before relationship, the final value of i should be strictly 2000000. However, the actual result is

Volatile fence demo?

坚强是说给别人听的谎言 提交于 2019-12-17 19:02:05
问题 Im trying to see how the fence is applied. I have this code (which Blocks indefinitely): static void Main() { bool complete = false; var t = new Thread(() => { bool toggle = false; while(!complete) toggle = !toggle; }); t.Start(); Thread.Sleep(1000); complete = true; t.Join(); // Blocks indefinitely } Writing volatile bool _complete; solve the issue . Acquire fence : An acquire-fence prevents other reads/writes from being moved before the fence; But if I illustrate it using an arrow ↓ ( Think

volatile variables as argument to function

会有一股神秘感。 提交于 2019-12-17 18:41:57
问题 Having this code: typedef volatile int COUNT; COUNT functionOne( COUNT *number ); int functionTwo( int *number ); I can't get rid of some warnings.. I get this warning 1 at functionOne prototype [Warning] type qualifiers ignored on function return type and I get this warning 2, wherever I call functionTwo with a COUNT pointer argument instead of an int pointer [Warning] cast discards qualifiers from pointer target type obviously variables/pointers can't be "cast" to volatile/un-volatile.. but

What is the difference between a static global and a static volatile variable?

…衆ロ難τιáo~ 提交于 2019-12-17 17:44:13
问题 I have used a static global variable and a static volatile variable in file scope, both are updated by an ISR and a main loop and main loop checks the value of the variable. here during optimization neither the global variable nor the volatile variable are optimized. So instead of using a volatile variable a global variable solves the problem. So is it good to use global variable instead of volatile? Any specific reason to use static volatile?? Any example program would be appreciable. Thanks

Difference between synchronization of field reads and volatile

只愿长相守 提交于 2019-12-17 16:44:40
问题 In a nice article with some concurrency tips, an example was optimized to the following lines: double getBalance() { Account acct = verify(name, password); synchronized(acct) { return acct.balance; } } If I understand that correctly, the point of the synchronization is to ensure that the value of acct.balance that are read by this thread is current and that any pending writes to the fields of the object in acct.balance are also written to main memory. The example made me think a little:

Why my boolean variable should be volatile while stopping thread loop? [duplicate]

心不动则不痛 提交于 2019-12-17 16:39:12
问题 This question already has answers here : What is the volatile keyword useful for (21 answers) Closed 3 years ago . Let's say I have a thread including while loop and I want to stop it "from outside". public class MyThread extends Thread { private boolean running = true; @Override public void run() { while (running) { // do something } } public void setRunning(boolean running) { this.running = running; } } And here is the Main class: public class Main { public static void main(String[] args) {