并发编程艺术-7

本文主要介绍了java 中的 原子操作类,分为原子更新基本类型,原子更新数组,原子更新引用,原子更新属性,基本上都是使用Unsafe 实现的包装类,因为比较简单,因此下面就拿代码直接解释了。

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 
 * @author Eric
 *
 */
public class AtomicClass {
    public static class People{
        
        String name;
        volatile int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public People(String name, Integer age) {
            super();
            this.name = name;
            this.age = age;
        }
    }
    
    public static volatile int count = 0;
    
    /**
     * 实现线程安全的几种方法
     */
    public static Integer num  = 0;
    public static Object obj = new Object();
    public static Lock lock = new ReentrantLock();

    public static AtomicInteger number  = new AtomicInteger();
    public static AtomicReference ref  = new AtomicReference();
    
    
    public static void main(String[] args) {
        
        new Thread(new Runnable(){

            @Override
            public void run() {
                for(int i = 0; i < 1000000; i ++){
                    try{
                    lock.lock();
                    count ++;
                    }catch(Exception e){}
                    finally {
                        lock.unlock();
                    }
                    number.getAndAdd(1);    
                }
            }}).start();
        new Thread(new Runnable(){
            @Override
            public void run() {
                for(int i = 0; i < 1000000; i ++){
                    try{
                    lock.lock();
                    count ++;
                    count -- ;
                    }catch(Exception e){}
                    finally {
                        lock.unlock();
                    }
                    number.getAndAdd(1);    
                }
            }}).start();
        while(Thread.activeCount()>1){
            Thread.yield();
        }
        People p1 = new People("aa",11);
        People p2 = new People("bb",22);
        /**
         * AtomicReference 更新类对象
         */
        ref.set(p1);
        ref.compareAndSet(p1, p2);
        //System.out.println(ref.get().getAge());
        
        /**
         * AtomicIntegerFieldUpdater 更新字段
         */
        AtomicIntegerFieldUpdater up = AtomicIntegerFieldUpdater.newUpdater(People.class, "age");
        up.getAndIncrement(p1);
        //System.out.println(p1.getAge());
        /**
         * AtomicIntegerArray 更新数组
         */
        int[] array = new int[]{1,2,3,4};
        AtomicIntegerArray arr = new AtomicIntegerArray(array);
        arr.getAndAdd(2, 2);
        System.out.println(arr.get(2)); 
    }
}

你可能感兴趣的:(并发编程艺术-7)