i++的线程不安全以及使用原子类保证线程安全

package com.xiangwen.day3;

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

public class AtomicTest {
    public static void main(String[] args) {
//        AtomicBoolean atomicFlg=new AtomicBoolean(false);
//        boolean res=atomicFlg.compareAndSet(false,true);
//        System.out.println(res);
//        System.out.println(atomicFlg);
//
//        AtomicInteger atomicInt=new AtomicInteger();
//        boolean res2=atomicInt.compareAndSet(10,123);
//        System.out.println(res2);
//        System.out.println(atomicInt);
//        Integer a=0;
//        atomicInt.addAndGet(2);
        UnsafeCounter UnsafeCounter=new UnsafeCounter();
         CountDownLatch c1=new CountDownLatch(200);
        ExecutorService executorService = Executors.newCachedThreadPool();

        for(int i=0;i<200;i++){
            new Thread(new ThreadTestInt(UnsafeCounter)).start();
            c1.countDown();
      }
        try {
           c1.await();
           Thread.sleep(1000);
           System.out.println("=======UnsafeCounter:"+UnsafeCounter.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//使用线程安全的原子类
    SafeCounter safeCounter=new SafeCounter();
    CountDownLatch c2=new CountDownLatch(200);
        for(int j=0;j<200;j++){
        new Thread(new ThreadTestAtomicInt(safeCounter)).start();
        c2.countDown();
    }
        try {
        c2.await();
        Thread.sleep(1000);
        System.out.println("=======safeCounter:"+safeCounter.get());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}
    class ThreadTestInt implements Runnable{
        private UnsafeCounter unsafeCounter;
        public ThreadTestInt(UnsafeCounter unsafeCounter) {
            this.unsafeCounter = unsafeCounter;
        }
        @Override
        public void run() {
            unsafeCounter.add();
        }
    }
class ThreadTestAtomicInt implements Runnable{
    private SafeCounter safeCounter;
    public ThreadTestAtomicInt(SafeCounter safeCounter) {
        this.safeCounter = safeCounter;
    }
    @Override
    public void run() {
        safeCounter.add();
    }
}
class UnsafeCounter {
    public int count = 0;
    public void add() {//没有加同步sychronized
        if(count%2==0){//for循环开启200个线程,因为i++运行很快,相当于顺序运行了,这样让线程稍微休眠,等其他线程创建出来竞争cpu
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count++;
    }
    public int get() {
        return count;
    }

}
class SafeCounter {
    public AtomicInteger count = new AtomicInteger(0);
    public void add() {
        if(count.get()%2==0){//for循环开启200个线程,因为i++运行很快,相当于顺序运行了,这样让线程稍微休眠,等其他线程创建出来竞争cpu
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count.addAndGet(1);
    }
    public int get() {
        return count.get();
    }

}
原文地址:https://www.cnblogs.com/wenwenzuiniucha/p/14807642.html