• 怎样去理解Java中的volatile


    volatile修饰符修饰变量,不可以修饰类和方法,volatile不是线程安全的,但是一写多读的多线程情况下是线程安全的。

    多线程的特性:

    •   可见性  各线程之间的变量会同步到主线程
    •   原子性

    volatile是可见的,但不是原子的。

    synchronized是可见的也是原子的。

    一个线程写,多个线程读可以使用volatile,这样的情况下的多线程是安全的。

    多个线程写,多个线程读不能使用volatile,这样的情况下的多线程是不安全的。

    package crelle.test.juc;
    
    /**
     * 一个线程写,多个线程读 线程安全
     */
    public class VolatileSingleWriterTest {
        private static volatile int MY_INT = 0;
        public static void main(String[] args) {
            new ChangeListener().start();
            new ChangeListener().start();
            new ChangeListener().start();
            new ChangeListener().start();
            new ChangeListener().start();
            new ChangeListener().start();
            new ChangeMaker().start();
        }
        static class ChangeListener extends Thread {
            @Override
            public void run() {
                int local_value = MY_INT;
                while ( local_value < 5){
                    if( local_value!= MY_INT){
                        System.out.println("Got Change for MY_INT : "+ MY_INT);
                        local_value= MY_INT;
                    }
                }
            }
        }
    
        static class ChangeMaker extends Thread{
            @Override
            public void run() {
                int local_value = MY_INT;
                while (MY_INT <5){
                    System.out.println("Incrementing MY_INT to : "+( local_value+1));
                    MY_INT = ++local_value;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) { e.printStackTrace(); }
                }
            }
        }
    }
    结果:
    Incrementing MY_INT to : 1
    Got Change for MY_INT : 1
    Got Change for MY_INT : 1
    Got Change for MY_INT : 1
    Got Change for MY_INT : 1
    Got Change for MY_INT : 1
    Got Change for MY_INT : 1
    Incrementing MY_INT to : 2
    Got Change for MY_INT : 2
    Got Change for MY_INT : 2
    Got Change for MY_INT : 2
    Got Change for MY_INT : 2
    Got Change for MY_INT : 2
    Got Change for MY_INT : 2
    Incrementing MY_INT to : 3
    Got Change for MY_INT : 3
    Got Change for MY_INT : 3
    Got Change for MY_INT : 3
    Got Change for MY_INT : 3
    Got Change for MY_INT : 3
    Got Change for MY_INT : 3
    Incrementing MY_INT to : 4
    Got Change for MY_INT : 4
    Got Change for MY_INT : 4
    Got Change for MY_INT : 4
    Got Change for MY_INT : 4
    Got Change for MY_INT : 4
    Got Change for MY_INT : 4
    Incrementing MY_INT to : 5
    Got Change for MY_INT : 5
    Got Change for MY_INT : 5
    Got Change for MY_INT : 5
    Got Change for MY_INT : 5
    Got Change for MY_INT : 5
    Got Change for MY_INT : 5
    
    Process finished with exit code 0
    

      

    package crelle.test.juc;
    
    /**
     * 多个线程写,多个线程读 线程不安全
     */
    public class VolatileMultiWriterTest {
        private static volatile int MY_INT = 0;
        public static void main(String[] args) {
            new ChangeListener().start();
            new ChangeMaker().start();
            new ChangeMaker().start();
            new ChangeMaker().start();
            new ChangeMaker().start();
            new ChangeMaker().start();
            new ChangeMaker().start();
        }
        static class ChangeListener extends Thread {
            @Override
            public void run() {
                int local_value = MY_INT;
                while ( local_value < 5){
                    if( local_value!= MY_INT){
                        System.out.println("Got Change for MY_INT : "+ MY_INT);
                        local_value= MY_INT;
                    }
                }
            }
        }
    
        static class ChangeMaker extends Thread{
            @Override
            public void run() {
                int local_value = MY_INT;
                while (MY_INT <5){
                    System.out.println("Incrementing MY_INT to : "+( local_value+1));
                    MY_INT = ++local_value;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) { e.printStackTrace(); }
                }
            }
        }
    }
    结果:
    Incrementing MY_INT to : 1 Incrementing MY_INT to : 1 Incrementing MY_INT to : 1 Got Change for MY_INT : 1 Incrementing MY_INT to : 2 Incrementing MY_INT to : 2 Incrementing MY_INT to : 2 Got Change for MY_INT : 2 Incrementing MY_INT to : 2 Incrementing MY_INT to : 2 Incrementing MY_INT to : 3 Incrementing MY_INT to : 2 Incrementing MY_INT to : 3 Incrementing MY_INT to : 3 Got Change for MY_INT : 3 Incrementing MY_INT to : 3 Incrementing MY_INT to : 3 Incrementing MY_INT to : 4 Incrementing MY_INT to : 3 Incrementing MY_INT to : 4 Incrementing MY_INT to : 4 Got Change for MY_INT : 4 Incrementing MY_INT to : 4 Incrementing MY_INT to : 4 Incrementing MY_INT to : 5 Got Change for MY_INT : 5 Incrementing MY_INT to : 4 Incrementing MY_INT to : 5 Incrementing MY_INT to : 5 Process finished with exit code 0

      

     

  • 相关阅读:
    C#获取当前程序运行路径的方法集合
    SQL为查询的结果加上序号(ROW_NUMBER) 合并多个查询结果
    Asp.net导出excel时长数字被科学计数法的解决方案。(身份证长数字作为字符处理)
    Stopwatch的用法
    对web.config的ConnectionString加密
    如何检索数据库中的空值和null
    如何将闲置的平板作为第二显示器(分屏)使用
    pyinstaller 打包文件太大
    如何让openssl生成的SSL证书被浏览器认可
    还是那该死的IE~~~
  • 原文地址:https://www.cnblogs.com/crelle/p/13304832.html
Copyright © 2020-2023  润新知