volatile修饰符

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
时间: 2024-09-29 11:20:59

volatile修饰符的相关文章

java中Volatile修饰符的含义

在java语言中:为了获得最佳速度,允许线程保存共享成员变量的私有拷贝,而且只当线程进入或者离开同步代码块时才与共享成员变量的原始值进行对比. volatile关键字的作用就是提示vm:对于这个成员变量不能保存它的私有拷贝,而应直接与共享变量进行交互. 被volatile修饰符修饰的成员变量在每次被线程访问时,都强迫从共享内存中重读该成员变量的值.而且,当成员变量发生变化时,又强迫线程将变化了的值写回共享内存,这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值.这样当多个线程同时与某个

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

STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay; 这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO原来是在Core_cm3.h中被重定义,其实就是volatile,句子如下 /* IO definitions (access restrictions to peripheral registers) */ #ifdef __cplusplus #define   __I     volatile

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

STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay;  这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO原来是在Core_cm3.h中被重定义,其实就是volatile,句子如下 /* IO definitions (access restrictions to peripheral registers) */ #ifdef __cplusplus #define   __I     volatile

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

转载请注明出处:     volatile用处说明     在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要. 在当前的Java内存模型下,线程可以把变量保存在本地内存(比如机器的寄存器)中,而不是直接在主存中进行读写.这就可能造成一个线程在主存中修改了一个变量的值,而另外一个线程还继续使用它在寄存器中的变量值的拷贝,造成数据的不一致. 要解决这个问题,就需要

c语言修饰符总结const、static、volatile、auto、extern、register

写在前面 今天下午一个同事问「register」关键字是什么作用?噢,你说的是「register」啊,它的作用是--脑袋突然断片儿,我擦,啥意思来着,这么熟悉的陌生感.做C语言开发时间也不短了,不过好像没有用到过「register」,但作用还是知道的,一下子想不起来了,一万个草泥马飞奔过来. 其实C语言中除了register外,还包含常见的const.static.volatile.auto.extern等修饰符,现在一起再总结一下好了. register 修饰符 register,寄存器变量,

Java并发编程(5):volatile变量修饰符-意料之外的问题(含代码)

volatile用处说明在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要. 在当前的Java内存模型下,线程可以把变量保存在本地内存(比如机器的寄存器)中,而不是直接在主存中进行读写.这就可能造成一个线程在主存中修改了一个变量的值,而另外一个线程还继续使用它在寄存器中的变量值的拷贝,造成数据的不一致. 要解决这个问题,就需要把变量声明为volatile(也可以

JavaSE入门学习10:Java修饰符

Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: <span style="font-size:18px;">public class className { // ... } private boolean flag; static final double weeks = 9.5; protected static final int BOXWIDTH = 42;

Java 修饰符

Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: public class className { // ... } private boolean myFlag; static final double weeks = 9.5; protected static final int BOXWIDTH = 42; public static void main(String[] argum

java学习手札(5)修饰符

java的修饰符分为访问修饰符和非访问修饰符 访问修饰符 可见的包,默认.不需要任何修饰??. 可见的类(private). 可见的世界(public). 可见包和所有子类 (protected). 非访问修饰符 static 修饰符用于创建类的方法和变量 final 修饰符的类最终确定,方法和变量的实现. abstract 修饰符用于创建抽象类和方法. synchronized 和 volatile 修饰符这是用于线程.