volatile对变量可见性和非原子性测试

1、关于valitile对变量的可见性测试

package com.delicacy.oatmeal.java.volatiletest;

/**
 * valitile dome
 * volatile 关键字保证了操作的可见性
 * valitie 最好用于一写多读的情况下
 * {@link VolatileDemo2} 测试volatile不能保证对变量的操作是原子性
 * @author zyt
 * @create 2018-04-15 12:53
 **/
public class VolatileDemo extends Thread{
    //设置类静态变量,各线程访问这同一共享变量
    private  static boolean flag = false;
    //无限循环,等待flag变为true时才跳出循环
    public void run() {
        while (!flag){
        };
        System.out.println("停止了");
    }

    public static void main(String[] args) throws Exception {
        new VolatileDemo().start();
        new VolatileDemo().start();
        new VolatileDemo().start();
        System.out.println("当前flag是"+flag);
        //sleep的目的是等待线程启动完毕,也就是说进入run的无限循环体了
        Thread.sleep(100);
        flag = true;
        System.out.println("当前flag是"+flag);
    }
}

2、volatile不能保证对变量的操作是原子性

package com.delicacy.oatmeal.java.volatiletest;

import java.util.concurrent.CountDownLatch;

/**
 * 测试volatile不能保证对变量的操作是原子性
 *
 * @author zyt
 * @create 2018-04-15 12:53
 **/
public class VolatileDemo2 extends Thread{
    public volatile int inc = 0;

    public /*synchronized*/ void increase() {
        inc++;
    }

    public static void main(String[] args) {
        process2();
    }

    private static void process1() {
        final VolatileDemo2 test = new VolatileDemo2();
        for(int i=0;i<10;i++){
            new Thread(() -> {
                for(int j=0;j<1000;j++)
                    test.increase();
            }).start();
        }

        while(Thread.activeCount()>1)  //保证前面的线程都执行完
            Thread.yield();
        System.out.println(test.inc);
    }

    private static void process2() {
        final int count = 10;
        final CountDownLatch latch = new CountDownLatch(count);
        final VolatileDemo2 test = new VolatileDemo2();
        for(int i=0; i<count; i++){
            new Thread(()->{
                for(int j=0;j<1000;j++){
                    test.increase();
                }
                latch.countDown();
            }).start();
        }
        try {
            latch.await();  //保证前面的线程都执行完
        } catch (InterruptedException e) {
        }
        System.out.println(test.inc);
    }

    private static void process3() {
        final VolatileDemo2 test = new VolatileDemo2();

        Thread thread = new Thread(() -> {
            for (int j = 0; j < 1000; j++)
                test.increase();
        });
        Thread thread2 = new Thread(() -> {
            for (int j = 0; j < 1000; j++)
                test.increase();
        });
        thread.start();
        thread2.start();

        try {
            thread.join(); //保证前面的线程都执行完
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(test.inc);
    }
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyt807/article/details/79948911
文章标签: volatile
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭