AtomicInteger类和int原生数据类型多线程下的应用

package com.atomic;

/**
 * Created by Liuxd on 2018-08-24.
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class TestAtomicInteger {
    public static final AtomicInteger atomicInteger = new AtomicInteger(0);
    public static Integer num = new Integer(0);
    public static Integer numForHaveLock = new Integer(0);

    private static void testAtomicInteger() {
        ExecutorService pool = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++)
            pool.execute(
                    new Thread(new Runnable() {
                        public void run() {
                            for (int j = 0; j < 2; j++) {
                                atomicInteger.incrementAndGet();
                                System.out.print(atomicInteger.get()+" ");
                            }
                        }
                    }) {
                    }
            );
        pool.shutdown();
        while (true) {
            if (pool.isTerminated()) {
                System.out.println();
                System.out.println("atomicInteger自带锁最终结果是" + atomicInteger.get());
                break;
            }
        }

    }

    private static synchronized void integerAdd() {
        numForHaveLock++;
    }

    private static void testIntegerHaveLock() {
        ExecutorService pool = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++)
            pool.execute(
                    new Thread(new Runnable() {
                        public void run() {
                            for (int j = 0; j < 2; j++) {
                                integerAdd();
                                System.out.print(numForHaveLock+" ");
                            }
                        }
                    }) {
                    }
            );
        pool.shutdown();

        while (true) {
            if (pool.isTerminated()) {
                System.out.println();
                System.out.println("有锁最终结果是" + numForHaveLock);
                break;
            }
        }

    }

    private static void testIntegerHaveNoLock() {
        ExecutorService pool = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++)
            pool.execute(
                    new Thread(new Runnable() {
                        public void run() {
                            for (int j = 0; j < 2; j++) {
                                num++;
                                System.out.print(num+" ");
                            }
                        }
                    }) {
                    }
            );
        pool.shutdown();

        while (true) {
            if (pool.isTerminated()) {
                System.out.println();
                System.out.println("无锁最终结果是" + num);
                break;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        testAtomicInteger();
        testIntegerHaveLock();
        testIntegerHaveNoLock();

    }
}

输出结果:

2 5 4 2 3 9 8 7 13 6 14 12 11 16 10 15 17 18 20 19 22 21 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
atomicInteger自带锁最终结果是40
1 2 3 4 5 6 8 7 9 10 11 12 13 14 15 16 17 18 19 20 21 23 23 24 25 26 27 30 30 29 32 31 34 34 35 36 37 38 39 40 
有锁最终结果是40
1 2 3 4 5 6 8 7 9 10 11 12 13 14 14 15 17 16 19 18 20 21 22 23 24 25 26 27 28 29 30 31 33 33 34 36 35 37 38 39 
无锁最终结果是39

猜你喜欢

转载自blog.csdn.net/jiahao1186/article/details/82012442
今日推荐