怎样去理解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

  

 

 

上一篇:javascript-Opencart向opencart添加产品选项


下一篇:PLT-hook技术