大致说明

一般比较多的组合是 volatile 读,synchronized 写方式 来确保线程安全
写也可采用

package com.finger.test.temp.thread;

import io.swagger.models.auth.In;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Des:
 * @Auther: 飞狐
 * @Date: 2019/3/3
 */
public class SaveThreadTest {

    //使用 volatile 确保能够获取到最新数据,单独使用 volatile不能确保原子性
    private static volatile Integer count = 0;

    //使用 同步锁,确保每次数据新增是独占的
    public synchronized void add(){
        count ++;
    }

    public Integer get(){
        return count;
    }


    public static void main(String[] args) {
        SaveThreadTest saveThreadTest = new SaveThreadTest();

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,50L, TimeUnit.SECONDS,new LinkedBlockingDeque<>());

        for(int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 100000; i++) {
                        saveThreadTest.add();
                    }
                }
            });

        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(saveThreadTest.get());

    }

}

采用 ReentrantLock 锁方式

package com.finger.test.temp.thread;

import io.swagger.models.auth.In;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Des:
 * @Auther: 飞狐
 * @Date: 2019/3/3
 */
public class SaveThreadTest {

    //使用 volatile 确保能够获取到最新数据,单独使用 volatile不能确保原子性
    private static volatile Integer count = 0;

    private static ReentrantLock lock = new ReentrantLock();

    //使用 ReentrantLock锁,确保每次数据新增是独占的
    public void add(){
        try {
            lock.lock();
            count ++;
        }catch (Exception e){
            e.getStackTrace();
        }finally {
            lock.unlock();
        }

    }

    public Integer get(){
        return count;
    }


    public static void main(String[] args) {
        SaveThreadTest saveThreadTest = new SaveThreadTest();

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,50L, TimeUnit.SECONDS,new LinkedBlockingDeque<>());

        for(int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 100000; i++) {
                        saveThreadTest.add();
                    }
                }
            });

        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(saveThreadTest.get());

    }

}


↙↙↙阅读原文可查看相关链接,并与作者交流