unsafe

本文详细介绍了如何通过反射获取并使用Java的Unsafe对象,展示了其在反射操作和CAS(Compare and Swap)中的应用,以及自行实现原子整数类的实例。探讨了底层内存管理和并发控制的高级技巧。
摘要由CSDN通过智能技术生成

unsafe

1.反射创建Unsafe对象

/**
 * Unsafe 对象提供了非常底层的,操作内存、线程的方法,Unsafe 对象不能直接调用,
 * 只能通过反射获得
 * private static final Unsafe theUnsafe;
 */
@Test
public void test_createUnsafeObj() throws NoSuchFieldException, IllegalAccessException {
    //反射获取私有的成员变量theUnsafe
    final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    //暴力反射,可以访问private
    theUnsafe.setAccessible(true);
    //静态不需要传递对象
    Unsafe unsafe = (Unsafe) theUnsafe.get(null);
    System.out.println(unsafe);
}

2. Unsafe的CAS操作

/**
 * Unsafe的Cas操作
 */
@Test
public void test_UnsafeCas() {
    try {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        Unsafe unsafe = (Unsafe) theUnsafe.get(null);
        //获取域偏移量
        long idOffset = unsafe.objectFieldOffset(Student.class.getDeclaredField("id"));
        long nameOffset = unsafe.objectFieldOffset(Student.class.getDeclaredField("name"));
        //cas修改student对象的id,name
        Student student = new Student();
        System.out.println(student);
        unsafe.compareAndSwapInt(student, idOffset, 0, 1);
        unsafe.compareAndSwapObject(student, nameOffset, null, "张三");
        System.out.println(student);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /**
     * Student{id=0, name='null'}
     * Student{id=1, name='张三'}
     */
}

class Student {
    private volatile int id;
    private volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

3.自己实现原子整数类

package com.concurrent.p7;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 利用Unsafe创建自己的AtomicInteger
 */
@Slf4j(topic = "c.Test_MyAtomicInteger")
public class Test_MyAtomicInteger {

    @Test
    public void test_MyAtomicInteger() {
        //定义共享账户变量
        MyAccount myAccount = new MyAccount(100000);
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            threadList.add(new Thread(() -> {
                for (int j = 0; j < 10; j++) {
                    myAccount.decrement(1000);
                }
            }));
        }
        threadList.forEach(t -> t.start());
        threadList.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        //输出账户最终余额
        log.debug("余额:{}", myAccount.getBalance());
    }

}

class MyAtomicInteger {
    //贡献变量
    private volatile int value;
    //成员对象偏移量
    private static long valueOffset;
    //unsafe对象
    private static Unsafe unsafe;

    static {
        try {
            //反射获取unsafe对象
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            unsafe = (Unsafe) theUnsafe.get(null);
            //获取偏移量
            valueOffset = unsafe.objectFieldOffset(MyAtomicInteger.class.getDeclaredField("value"));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public MyAtomicInteger(int value) {
        this.value = value;
    }

    //获取数值
    public int getValue() {
        return value;
    }

    //cas减操作
    public boolean decrement(int v) {
        int prev, next;
        do {
            prev = getValue();
            next = prev - v;
        } while (!unsafe.compareAndSwapInt(this, valueOffset, prev, next));
        return true;
    }
}

//账户类
class MyAccount {
    private MyAtomicInteger balance;

    public MyAccount(int balance) {
        this.balance = new MyAtomicInteger(balance);
    }

    public void decrement(int value) {
        balance.decrement(value);
    }

    public int getBalance() {
        return balance.getValue();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值