美文网首页
从多线程下自增操作来看高并发中共享变量安全性

从多线程下自增操作来看高并发中共享变量安全性

作者: 有鱼哎 | 来源:发表于2018-08-30 19:15 被阅读0次

    不做任何安全措施下的代码

    public class IncrementTest {
    private static int inc = 0;
    private static void increase() {
        inc++;
    }
    public static void main(String[] params) throws Exception {
        for (int num = 0; num < 10; num++) {
            Thread firstThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread secThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread thirdThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            firstThread.start();
            secThread.start();
            thirdThread.start();
            firstThread.join();
            secThread.join();
            thirdThread.join();
            System.out.print(inc + " ");
            inc = 0;
        }
    }  }  //result:280957 300000 300000 265487 300000 300000 300000 300000 300000 300000 
    

    弱同步 volatile

    public class IncrementTest {
    private static volatile int inc = 0;
    private static void increase() {
        inc++;
    }
    public static void main(String[] params) throws Exception {
        for (int num = 0; num < 10; num++) {
            Thread firstThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread secThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread thirdThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            firstThread.start();
            secThread.start();
            thirdThread.start();
            firstThread.join();
            secThread.join();
            thirdThread.join();
            System.out.print(inc + " ");
            inc = 0;
        }
    }}  //result:271160 300000 300000 219991 171981 300000 300000 300000 300000 265862
    

    互斥同步(悲观锁)synchronized

    public class IncrementTest {
    private static  int inc = 0;
    private synchronized static void increase() {
        inc++;
    }
    public static void main(String[] params) throws Exception {
        for (int num = 0; num < 10; num++) {
            Thread firstThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread secThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread thirdThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            firstThread.start();
            secThread.start();
            thirdThread.start();
            firstThread.join();
            secThread.join();
            thirdThread.join();
            System.out.print(inc + " ");
            inc = 0;
        }
    }}  //result:300000 300000 300000 300000 300000 300000 300000 300000 300000 300000
    

    非阻塞同步(乐观锁)

     public class IncrementTest {
    private static AtomicInteger inc = new AtomicInteger(0);
    private synchronized static void increase() {
        inc.getAndIncrement();
    }
    public static void main(String[] params) throws Exception {
        for (int num = 0; num < 10; num++) {
            Thread firstThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread secThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            Thread thirdThread = new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    increase();
                }
            });
            firstThread.start();
            secThread.start();
            thirdThread.start();
            firstThread.join();
            secThread.join();
            thirdThread.join();
            System.out.print(inc + " ");
            inc.set(0);
        }
    }}  //result:300000 300000 300000 300000 300000 300000 300000 300000 300000 300000

    相关文章

      网友评论

          本文标题:从多线程下自增操作来看高并发中共享变量安全性

          本文链接:https://www.haomeiwen.com/subject/bsbdwftx.html