zoukankan      html  css  js  c++  java
  • 怎样去理解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

      

     

  • 相关阅读:
    蓝桥杯 全球变暖(dfs)
    Bzoj3196 Tyvj 1730 二逼平衡树
    Bzoj3110 [Zjoi2013]K大数查询
    Bzoj4004 [JLOI2015]装备购买
    Bzoj2115 [Wc2011] Xor
    Bzoj1257 [CQOI2007]余数之和sum
    HDU1724 Ellipse
    Bzoj2178 圆的面积并
    SPOJ CIRU The area of the union of circles
    CodeForces 232E.Quick Tortoise
  • 原文地址:https://www.cnblogs.com/crelle/p/13304832.html
Copyright © 2011-2022 走看看