天天看點

FieldUpdater

FieldUpdater

一般我們在多線程環境下,更新一個對象的字段,我們會對操作對象的方法進行加鎖,讓線程串行,進而達到線程安全的目的。但加鎖不當,可能使程式性能不佳,或者引起死鎖問題。如果不加鎖,那麼可以使用FieldUpdater 進行操作字段,也能夠達到線程安全的目的。

FieldUpdater 在

java.util.concurrent.atomic

包中,由三個比較特殊的原子類:

AtomicIntegerFieldUpdater

AtomicLongFieldUpdater

AtomicReferenceFieldUpdater

通過名稱可以看到,這幾類的功能大緻相同,隻是針對的類型有所不同。所謂AtomicXXXFieldUpdater,就是可以以一種線程安全的方式操作非線程安全對象的某些字段。

AtomicReferenceFieldUpdater

AtomicReferenceFieldUpdater 使用

AtomicReferenceFieldUpdater本身是一個抽象類,沒有公開的構造器,隻能通過靜态方法newUpdater建立一個AtomicReferenceFieldUpdater子類對象:

newUpdater有三個入參

入參名稱 含義
tclass 目标對象的類型
vclass 目标字段的類型
fieldName 目标字段名
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass,
                                                                    Class<W> vclass,
                                                                    String fieldName) {
        return new AtomicReferenceFieldUpdaterImpl<U,W>
            (tclass, vclass, fieldName, Reflection.getCallerClass());
    }
           
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                        final Class<V> vclass,
                                        final String fieldName,
                                        final Class<?> caller) {
            final Field field;
            final Class<?> fieldClass;
            final int modifiers;
            try {
                field = AccessController.doPrivileged(
                    new PrivilegedExceptionAction<Field>() {
                        public Field run() throws NoSuchFieldException {
                            return tclass.getDeclaredField(fieldName);
                        }
                    });
                modifiers = field.getModifiers();
                sun.reflect.misc.ReflectUtil.ensureMemberAccess(
                    caller, tclass, null, modifiers);
                ClassLoader cl = tclass.getClassLoader();
                ClassLoader ccl = caller.getClassLoader();
                if ((ccl != null) && (ccl != cl) &&
                    ((cl == null) || !isAncestor(cl, ccl))) {
                    sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
                }
                fieldClass = field.getType();
            } catch (PrivilegedActionException pae) {
                throw new RuntimeException(pae.getException());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }

            if (vclass != fieldClass)
                throw new ClassCastException();
            if (vclass.isPrimitive())
                //必須是引用類型,不能是基本類型
                throw new IllegalArgumentException("Must be reference type");

            if (!Modifier.isVolatile(modifiers))
                // 必須用volatile修飾
                throw new IllegalArgumentException("Must be volatile type");

            // Access to protected field members is restricted to receivers only
            // of the accessing class, or one of its subclasses, and the
            // accessing class must in turn be a subclass (or package sibling)
            // of the protected member's defining class.
            // If the updater refers to a protected field of a declaring class
            // outside the current package, the receiver argument will be
            // narrowed to the type of the accessing class.
            this.cclass = (Modifier.isProtected(modifiers) &&
                           tclass.isAssignableFrom(caller) &&
                           !isSamePackage(tclass, caller))
                          ? caller : tclass;
            this.tclass = tclass;
            this.vclass = vclass;
            this.offset = U.objectFieldOffset(field);
        }
           

AtomicReferenceFieldUpdater**的使用條件

  1. AtomicReferenceFieldUpdater隻能修改對于它可見的字段,也就是說對于目标類的某個字段field,如果修飾符是private,但是AtomicReferenceFieldUpdater所在的使用類不能看到field,那就會報錯;
  2. 目标類的操作字段,必須用volatile修飾;
  3. 目标類的操作字段,不能是static的;
  4. AtomicReferenceFieldUpdater隻适用于引用類型的字段;

AtomicReferenceFieldUpdater的方法原理

AtomicReferenceFieldUpdater中所有的方法都是基于Unsafe類操作,通過偏移量offset擷取字段的位址,然後利用Unsafe進行CAS更新

public final boolean compareAndSet(T obj, V expect, V update) {
            accessCheck(obj);
            valueCheck(update);
            return U.compareAndSwapObject(obj, offset, expect, update);
        }
           

AtomicReferenceFieldUpdater 案例

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class TestAtomicReferenceFieldUpdater {

    public static void main(String[] args) {
        AtomicReferenceFieldUpdater updater= AtomicReferenceFieldUpdater.newUpdater(Person .class, String.class, "name");
        Person person = new Person();
        // 如果 person 的name 屬性是 jim  那麼更新 name 為marry
        boolean b = updater.compareAndSet(person, "jim", "marry");
        System.out.println(b); // true 
        System.out.println(person.name); // marry
        // 擷取 name 的值
        Object o = updater.get(person); 
        System.out.println(o); // marry

        // 如果 person 的name 屬性是 jim  那麼更新 name 為marry,此時的name 是marry,是以更新會失敗
        boolean c = updater.compareAndSet(person, "jim", "tom");
        System.out.println(c); // false
        System.out.println(person.name); // marry
    }
}

class Person{
    volatile String name = "jim";
}
           

輸出結果

true
marry
marry
false
marry
           

AtomicIntegerFieldUpdater

AtomicIntegerFieldUpdater

AtomicIntegerFieldUpdater 的newUpdater 方法和AtomicReferenceFieldUpdater 類似,隻不過AtomicIntegerFieldUpdater處理的是int 類型的是以少一個字段類型參數,

newUpdater有三個入參

入參名稱 含義
tclass 目标對象的類型
fieldName 目标字段名
public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass,
                                                              String fieldName) {
        return new AtomicIntegerFieldUpdaterImpl<U>
            (tclass, fieldName, Reflection.getCallerClass());
    }
           

AtomicIntegerFieldUpdater的使用條件

  1. AtomicIntegerFieldUpdater隻能修改對于它可見的字段,也就是說對于目标類的某個字段field,如果修飾符是private,但是AtomicIntegerFieldUpdater所在的使用類不能看到field,那就會報錯;
  2. 目标類的操作字段,必須用volatile修飾;
  3. 目标類的操作字段,不能是static的;
  4. AtomicIntegerFieldUpdater隻适用于int類型的字段;

AtomicIntegerFieldUpdater案例

假設有一個公司賬戶Account,100個人同時往裡面存錢1塊錢,那麼正常情況下,最終賬戶的總金額應該是100。

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author Jim Huang
 * @date 2021/5/27 10:48
 */
public class TestAtomicIntegerFieldUpdater {
    public static void main(String[] args) throws Exception{
        Account account = new Account(0);

        List<Thread> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Thread t = new Thread(new Task(account));
            list.add(t);
            t.start();
        }

        for (Thread t : list) {
            t.join();
        }

        System.out.println(account.toString());
    }

   static class Account {
        private volatile int money;
        private static final AtomicIntegerFieldUpdater<Account> updater = AtomicIntegerFieldUpdater.newUpdater(Account.class, "money");  // 引入AtomicIntegerFieldUpdater

        Account(int initial) {
            this.money = initial;
        }

        public void increMoney() {
            updater.incrementAndGet(this);    // 通過AtomicIntegerFieldUpdater操作字段
        }

        public int getMoney() {
            return money;
        }

        @Override
        public String toString() {
            return "Account{" +
                    "money=" + money +
                    '}';
        }
    }

    private static class Task implements Runnable {
        private Account account;

        Task(Account account) {
            this.account = account;
        }

        @Override
        public void run() {
            account.increMoney();
        }
    }
}

           

執行結果