Java Unsafe 测试代码

2021-04-01 00:25

阅读:432

标签:list   unsafe   getname   java   count   return   array   about   init   

 

Java Unsafe 测试代码

import com.User;
import org.junit.Before;
import org.junit.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

class User {

    public static String USER_CLASS_NAME = "User.class";
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class LockTests {

    Unsafe unSafe;
    User u = new User(17, "zhangsan");

    @Before
    public void before() throws Exception {
        Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeField.setAccessible(true);
        unSafe = (Unsafe) theUnsafeField.get(Unsafe.class);
    }

    @Test
    public void objectFieldOffset() throws Exception {
        // unSafe偏底层的一个Java工具类
        java.util.List users = new ArrayList();
        for (int i = 0; i obj.
             * @return
             */
            int age = unSafe.getIntVolatile(u, 12L);
            System.out.println("age = " + age);
        }
    }

    // 系统负载采样的接口
    @Test
    public void getLoadAverage() {
        double[] nums = new double[8];
        int val = unSafe.getLoadAverage(nums, 8);
        System.out.println(val);
    }


    /**
     * //内存屏障,禁止load操作重排序。屏障前的load操作不能被重排序到屏障后,屏障后的load操作不能被重排序到屏障前
     * public native void loadFence();
     * 

*

* 参见:https://tech.meituan.com/2019/02/14/talk-about-java-magic-class-unsafe.html */ @Test public void loadFence() { //java.util.concurrent.locks.StampedLock.validate unSafe.loadFence(); } /** * //内存屏障,禁止store操作重排序。屏障前的store操作不能被重排序到屏障后,屏障后的store操作不能被重排序到屏障前 * public native void storeFence(); * 参见:https://tech.meituan.com/2019/02/14/talk-about-java-magic-class-unsafe.html */ @Test public void storeFence() { } /** * //内存屏障,禁止load、store操作重排序 * public native void fullFence(); * 参见:https://tech.meituan.com/2019/02/14/talk-about-java-magic-class-unsafe.html */ @Test public void fullFence() { } @Test public void shouldBeInitialized() { boolean shouldBeInitialized = unSafe.shouldBeInitialized(String.class); System.out.println(shouldBeInitialized); shouldBeInitialized = unSafe.shouldBeInitialized(User.class); System.out.println(shouldBeInitialized); } /** * synchronized 的一种实现获取锁 * * @throws InterruptedException */ @Test public void monitorEnter() throws InterruptedException { unSafe.monitorEnter(u); new Thread(new Runnable() { @Override public void run() { synchronized (u) { System.out.println("==u lock got =="); } } }).start(); Thread.sleep(2000); unSafe.monitorExit(u); } @Test public void compareAndSwap() { // unSafe.compareAndSwapInt(对象, 对象中的字段偏移, 期望值, 设置值) // unSafe.compareAndSwapLong(对象, 对象中的字段偏移, 期望值, 设置值) // unSafe.compareAndSwapObject(对象, 对象中的字段偏移, 期望值, 设置值) } @Test public void t() { // 方法签名 // public void copyMemory(Object srcBase, long srcOffset,Object destBase, long destOffset, long bytes) // unSafe.copyMemory(); } } class ClassIsLoad { static { System.out.println("ClassIsLoad class Is Load !"); } }

  

Java Unsafe 测试代码

标签:list   unsafe   getname   java   count   return   array   about   init   

原文地址:https://www.cnblogs.com/leodaxin/p/13556615.html


评论


亲,登录后才可以留言!