Volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值。而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。

一个volatile对象引用可能是null。

(1)Java程序代码

package niukewang;

public class volatile_Test {

    public static void main(String args[]) {
MyRunnable r = new MyRunnable();
Thread t1 = new Thread(r);
t1.start(); try {
Thread.sleep(2);
r.stopThread();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} class MyRunnable implements Runnable {
private volatile boolean active; public void run() {
active = true;
while (active) // 第一行
{
// 代码
System.out.println("Print the line");
}
} public void stopThread() {
System.out.println("Set to false");
active = false; // 第二行
}
}

只有当调用stopThread时候,设置了ative为false的时候,然后线程获取了false的值,会停止第一行中内容的输出。

(2)输出结果。

Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Print the line
Set to false
Print the line

volatile修饰符的更多相关文章

  1. Java 的 volatile 修饰符

    volatile 修饰符,用于多线程同步 volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值.而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存.这 ...

  2. Java中volatile修饰符,不稳定标记的用法笔记

    今天学java特性时,发现了volatile修饰符,这个修饰符修饰的变量告诉java编译器忽略优化机制,这样的优势是: java优化后,寄存器会缓存内存里的变量,另一个线程修改这个变量的内存时,不会同 ...

  3. java中Volatile修饰符的含义

    在java语言中:为了获得最佳速度,同意线程保存共享成员变量的私有拷贝.并且仅仅当线程进入或者离开同步代码块时才与共享成员变量的原始值进行对照. volatilekeyword的作用就是提示vm:对于 ...

  4. 关于STM32库中 __IO 修饰符(volatile修饰符,反复无常的意思)

    STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay;  这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO ...

  5. STM32库中 __IO 修饰符(volatile修饰符)

    STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay; 这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO原 ...

  6. volatile 修饰符的有过什么实践?

    一种实践是用 volatile 修饰 long 和 double 变量,使其能按原子类型来读写. double 和 long 都是 64 位宽,因此对这两种类型的读是分为两部分的,第一次 读取第一个  ...

  7. 三个线程,ABC 10次(volatile+synchronized(2 synchronized可以保证内存可见性,所以去掉status 的volatile修饰符)

    package ThreadABC; public class MyThread extends Thread { public static int status = 0; @Override pu ...

  8. 转: 【Java并发编程】之五:volatile变量修饰符—意料之外的问题(含代码)

    转载请注明出处:     volatile用处说明     在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程 ...

  9. 【Java并发编程】:volatile变量修饰符

    volatile用处说明     在JDK1.2之前,java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键 ...

随机推荐

  1. PHP基础知识第二趴

    下期预告,函数...

  2. Java工程师成神之路

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:279558494 我们一起学Java! 一.基础篇 1.1 JVM 1.1.1. J ...

  3. Java之多态(一)

    package test05; public class DuoTai_Test { /** * 一个对象,多种形态 * WQQ → Student.Worker.Friend 1).一个对象,多种形 ...

  4. js picker webapp仿ios picker

    iosselect 在webapp下的一个picker组件 可以轻松实现各类选择器效果.比如地区选择 时间选择 日期选择等. 可以定制依赖关系,可以定制选择层级,可以定制高度 展示项数.无论你是px还 ...

  5. push方法的兼容性问题

    在IE8及以下中,不支持aplly方法中的第二个参数是 伪数组 需要对push方法进行封装. 将 push 的判断,放入一个沙箱中: 好处:在页面加载的时候就会执行这段代码,保证了代码只会检测一次 以 ...

  6. [Android]使用Dagger 2依赖注入 - 自定义Scope(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5095426.html 使用Dagger 2依赖注入 - 自定义 ...

  7. iOS 小谈开发者中的个人、组织(公司、企业)账号

    苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...

  8. Android 手机卫士--绑定sim卡序列号

    现在开始具体 处理每一个导航页面的逻辑,首先看第二个导航页 本文地址:http://www.cnblogs.com/wuyudong/p/5949775.html,转载请注明出处. 这里需要实现绑定s ...

  9. nginx实现单服务代理多域名

    通过一台nginx服务器代理多个域名进行跳转,原理很简单,重点在玩法!适用于公司处理域名紧急备案问题. 域名: www.hx123.com www.hx456.com nginx服务器: ginx.c ...

  10. jsonp跨域请求

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...