zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    MongoDB的安装和常用命令
    mysql安装、使用与遇见的问题汇总
    devicePixelRatio,Viewport,移动端适配
    javascript 数组以及对象的深拷贝(复制数组或复制对象)的方法
    正则表达式
    npm 常用命令
    Markdown 基本语法
    mysql忘记root密码
    mysql5.7.12/13在安装新实例时报错:InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero
    mysqld数据位于a盘,执行delete from table, 发现另外2个盘磁盘使用率接近100%,而a盘的使用率反而很低,y??
  • 原文地址:https://www.cnblogs.com/twodog/p/12141439.html
Copyright © 2011-2022 走看看