• volatile 对于n=n+1,无效


    1.volatile 针对b=b+1 不是原子操作的

      理论上来说结果等于1000,实际上很多时候小于1000

    package com.sun.util;
    class A {
        volatile int b=0;
    
           void add(int n){
            for (int i = 0; i < n; i++) {
                b+=1;
            }
        }
       
    }
    public class VolatileTest {
        
    
        public static void main(String[] args) {
            A aaa = new A();
            Thread[] t=new Thread[100];
            for (int i = 0; i < t.length; i++) {
                final int x=10;
                t[i]=new Thread(()->aaa.add(10));
            }
            for (int i = 0; i < t.length; i++) {
                t[i].start();
            }
            for (int i = 0; i < t.length; i++) {
                try {
                    t[i].join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(aaa.b);
    
    
        }
    }

    2.用synchronized

    package com.sun.util;
    class A {
        volatile int b=0;
    
        void add(int n){
            for (int i = 0; i < n; i++) {
    //            b+=1;
                inc();
            }
        }
        synchronized void inc(){
            b=b+1;
        }
    
       
        
    
    }
    public class VolatileTest {
        
    
        public static void main(String[] args) {
            A aaa = new A();
            Thread[] t=new Thread[100];
            for (int i = 0; i < t.length; i++) {
                final int x=i+1;
                t[i]=new Thread(()->aaa.add(x));
            }
            for (int i = 0; i < t.length; i++) {
                t[i].start();
            }
            for (int i = 0; i < t.length; i++) {
                try {
                    t[i].join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(aaa.b);
    
    
    
        }
    }

    或者

    package com.sun.util;
    class A {
        volatile int b=0;
    
        synchronized void add(int n){
            for (int i = 0; i < n; i++) {
                b+=1;
    //            inc();
            }
        }
    //    synchronized void inc(){
    //        b=b+1;
    //    }
    
       
        
    
    }
    public class VolatileTest {
        
    
        public static void main(String[] args) {
            A aaa = new A();
            Thread[] t=new Thread[100];
            for (int i = 0; i < t.length; i++) {
                final int x=i+1;
                t[i]=new Thread(()->aaa.add(x));
            }
            for (int i = 0; i < t.length; i++) {
                t[i].start();
            }
            for (int i = 0; i < t.length; i++) {
                try {
                    t[i].join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(aaa.b);
    
        }
    }
  • 相关阅读:
    PHP Session 变量
    PHP Cookie是什么
    PHP 文件上传
    PHP 文件处理
    PHP include 和 require 语句
    Mac pycharm专业版安装以及破解方法
    bzoj3946: 无聊的游戏
    win10 uwp 九幽图床
    win10 uwp 九幽图床
    git无法pull仓库refusing to merge unrelated histories
  • 原文地址:https://www.cnblogs.com/sunupo/p/13496894.html
Copyright © 2020-2023  润新知