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

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

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

你可能感兴趣的:(从多线程下自增操作来看高并发中共享变量安全性)