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);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值