Java Unsafe知识点有哪些

发布时间:2021-11-12 15:22:54 作者:iii
来源:亿速云 阅读:108

本篇内容介绍了“Java Unsafe知识点有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

sun.misc.Unsafe

作用:可以用来在任意内存地址位置处读写数据,支持一些CAS原子操作

Java最初被设计为一种安全的受控环境。尽管如此,HotSpot还是包含了一个后门sun.misc.Unsafe,提供了一些可以直接操控内存和线程的底层操作。Unsafe被JDK广泛应用于java.nio和并发包等实现中,这个不安全的类提供了一个观察HotSpot JVM内部结构并且可以对其进行修改,但是不建议在生产环境中使用

获取Unsafe实例

Unsafe对象不能直接通过new Unsafe()或调用Unsafe.getUnsafe()获取,原因如下:

@CallerSensitive
public static Unsafe getUnsafe() {
    Class var0 = Reflection.getCallerClass();
    if (!VM.isSystemDomainLoader(var0.getClassLoader())) {
        throw new SecurityException("Unsafe");
    } else {
        return theUnsafe;
    }
}

获取实例

//方法一:我们可以令我们的代码“受信任”。运行程序时,使用bootclasspath选项,指定系统类路径加上你使用的一个Unsafe路径
java -Xbootclasspath:/usr/jdk1.7.0/jre/lib/rt.jar:. com.mishadoff.magic.UnsafeClient
// 方法二
static {
    try {
        Field field = Unsafe.class.getDeclaredField("theUnsafe");
        field.setAccessible(true);
        UNSAFE = (Unsafe) field.get(null);
    } catch (Exception e) {
    }
}

注意:忽略你的IDE。比如:eclipse显示”Access restriction…”错误,但如果你运行代码,它将正常运行。如果这个错误提示令人烦恼,可以通过以下设置来避免:

Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference -> Warning

重点API

User instance = (User) UNSAFE.allocateInstance(User.class);
package com.quancheng;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class CollectionApp {
    private static sun.misc.Unsafe UNSAFE;
    public static void main(String[] args) {
        try {
            User instance = (User) UNSAFE.allocateInstance(User.class);
            instance.setName("luoyoub");
            System.err.println("instance:" + instance);
            instance.test();
            Field name = instance.getClass().getDeclaredField("name");
            UNSAFE.putObject(instance, UNSAFE.objectFieldOffset(name), "huanghui");
            instance.test();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            UNSAFE = (Unsafe) field.get(null);
        } catch (Exception e) {
        }
    }
}
class User {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void test() {
        System.err.println("hello,world" + name);
    }
}

使用场景

避免初始化

当你想要跳过对象初始化阶段,或绕过构造器的安全检查,或实例化一个没有任何公共构造器的类,allocateInstance方法是非常有用的,使用构造器、反射和unsafe初始化它,将得到不同的结果

public class CollectionApp {
    private static sun.misc.Unsafe UNSAFE;
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        A a = new A();
        a.test(); // output ==> 1
        A a1 = A.class.newInstance();
        a1.test(); // output ==> 1
        A instance = (A) UNSAFE.allocateInstance(A.class);
        instance.test(); // output ==> 0
    }
    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            UNSAFE = (Unsafe) field.get(null);
        } catch (Exception e) {
        }
    }
}
class A{
    private long a;
    public A(){
        a = 1;
    }
    public void test(){
        System.err.println("a==>" + a);
    }
}

内存崩溃(Memory corruption)

Unsafe可用于绕过安全的常用技术,直接修改内存变量;实际上,反射可以实现相同的功能。但值得关注的是,我们可以修改任何对象,甚至没有这些对象的引用

Guard guard = new Guard();
guard.giveAccess();   // false, no access
// bypass
Unsafe unsafe = getUnsafe();
Field f = guard.getClass().getDeclaredField("ACCESS_ALLOWED");
unsafe.putInt(guard, unsafe.objectFieldOffset(f), 42); // memory corruption
guard.giveAccess(); // true, access granted

注意:我们不必持有这个对象的引用

我们可以在运行时创建一个类,比如从已编译的.class文件中。将类内容读取为字节数组,并正确地传递给defineClass方法;当你必须动态创建类,而现有代码中有一些代理, 这是很有用的

private static byte[] getClassContent() throws Exception {
    File f = new File("/home/mishadoff/tmp/A.class");
    FileInputStream input = new FileInputStream(f);
    byte[] content = new byte[(int)f.length()];
    input.read(content);
    input.close();
    return content;
}
byte[] classContents = getClassContent();
Class c = getUnsafe().defineClass(
              null, classContents, 0, classContents.length);
c.getMethod("a").invoke(c.newInstance(), null); // 1

抛出异常(Throw an Exception)

该方法抛出受检异常,但你的代码不必捕捉或重新抛出它,正如运行时异常一样

getUnsafe().throwException(new IOException());

大数组(Big Arrays)

正如你所知,Java数组大小的最大值为Integer.MAX_VALUE。使用直接内存分配,我们创建的数组大小受限于堆大小;实际上,这是堆外内存(off-heap memory)技术,在java.nio包中部分可用;

这种方式的内存分配不在堆上,且不受GC管理,所以必须小心Unsafe.freeMemory()的使用。它也不执行任何边界检查,所以任何非法访问可能会导致JVM崩溃

class SuperArray {
    private final static int BYTE = 1;
    private long size;
    private long address;
    public SuperArray(long size) {
        this.size = size;
        address = getUnsafe().allocateMemory(size * BYTE);
    }
    public void set(long i, byte value) {
        getUnsafe().putByte(address + i * BYTE, value);
    }
    public int get(long idx) {
        return getUnsafe().getByte(address + idx * BYTE);
    }
    public long size() {
        return size;
    }
}
long SUPER_SIZE = (long)Integer.MAX_VALUE * 2;
SuperArray array = new SuperArray(SUPER_SIZE);
System.out.println("Array size:" + array.size()); // 4294967294
for (int i = 0; i < 100; i++) {
    array.set((long)Integer.MAX_VALUE + i, (byte)3);
    sum += array.get((long)Integer.MAX_VALUE + i);
}
System.out.println("Sum of 100 elements:" + sum);  // 300

并发(Concurrency)

几句关于Unsafe的并发性。compareAndSwap方法是原子的,并且可用来实现高性能的、无锁的数据结构

挂起与恢复

定义:

public native void unpark(Thread jthread);  
public native void park(boolean isAbsolute, long time); // isAbsolute参数是指明时间是绝对的,还是相对的

将一个线程进行挂起是通过park方法实现的,调用park后,线程将一直阻塞直到超时或者中断等条件出现。unpark可以终止一个挂起的线程,使其恢复正常。整个并发框架中对线程的挂起操作被封装在 LockSupport类中,LockSupport类中有各种版本pack方法,但最终都调用了Unsafe.park()方法;

unpark函数为线程提供“许可(permit)”,线程调用park函数则等待“许可”。这个有点像信号量,但是这个“许可”是不能叠加的,“许可”是一次性的;比如线程B连续调用了三次unpark函数,当线程A调用park函数就使用掉这个“许可”,如果线程A再次调用park,则进入等待状态,见下例Example1

Example1:
// 针对当前线程已经调用过unpark(多次调用unpark的效果和调用一次unpark的效果一样)
public static void main(String[] args) throws InterruptedException {
    Thread currThread = Thread.currentThread();
    UNSAFE.unpark(currThread);
    UNSAFE.unpark(currThread);
    UNSAFE.unpark(currThread);
    UNSAFE.park(false, 0);
    UNSAFE.park(false, 0);
    System.out.println("SUCCESS!!!");
}
// 恢复线程interrupt() && UNSAFE.unpark()运行结果一样
public static void main(String[] args) throws InterruptedException {
    Thread currThread = Thread.currentThread();
    new Thread(()->{
        try {
            Thread.sleep(3000);
            System.err.println("sub thread end");
            // currThread.interrupt();
            UNSAFE.unpark(currThread);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }).start();
    UNSAFE.park(false, 0);
    System.out.println("SUCCESS!!!");
}
// 如果是相对时间也就是isAbsolute为false(注意这里后面的单位纳秒)到期的时候,与Thread.sleep效果相同,具体有什么区别有待深入研究
//相对时间后面的参数单位是纳秒
UNSAFE.park(false, 3000000000l);
System.out.println("SUCCESS!!!");
long time = System.currentTimeMillis()+3000;
//绝对时间后面的参数单位是毫秒
UNSAFE.park(true, time);
System.out.println("SUCCESS!!!");

注意,unpark函数可以先于park调用。比如线程B调用unpark函数,给线程A发了一个“许可”,那么当线程A调用park时,它发现已经有“许可”了,那么它会马上再继续运行。实际上,park函数即使没有“许可”,有时也会无理由地返回,实际上在SUN Jdk中,object.wait()也有可能被假唤醒;

注意:unpark方法最好不要在调用park前对当前线程调用unpark

Unsafe API

sun.misc.Unsafe类包含105个方法。实际上,对各种实体操作有几组重要方法,其中的一些如下:
Info.仅返回一些低级的内存信息
addressSize
pageSize
Objects.提供用于操作对象及其字段的方法
allocateInstance     ##直接获取对象实例
objectFieldOffset
Classes.提供用于操作类及其静态字段的方法
staticFieldOffset
defineClass
defineAnonymousClass
ensureClassInitialized
Arrays.操作数组
arrayBaseOffset
arrayIndexScale
Synchronization.低级的同步原语
monitorEnter
tryMonitorEnter
monitorExit
compareAndSwapInt
putOrderedInt
Memory.直接内存访问方法
allocateMemory
copyMemory
freeMemory
getAddress
getInt
putInt

知识点

Unsafe.park()当遇到线程终止时,会直接返回(不同于Thread.sleep,Thread.sleep遇到thread.interrupt()会抛异常)

// Thread.sleep会抛异常
public static void main(String[] args) throws InterruptedException {
   Thread thread =  new Thread(()->{
        try {
            System.err.println("sub thread start");
            Thread.sleep(10000);
            System.err.println("sub thread end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
   thread.start();
    TimeUnit.SECONDS.sleep(3);
    thread.interrupt();
    System.out.println("SUCCESS!!!");
}
output==>
sub thread start
SUCCESS!!!
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at com.quancheng.ConcurrentTest.lambda$main$0(ConcurrentTest.java:13)
    at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0
public static void main(String[] args) throws InterruptedException {
   Thread thread =  new Thread(()->{
       System.err.println("sub thread start");
       UNSAFE.park(false,0);
       System.err.println("sub thread end");
    });
   thread.start();
    TimeUnit.SECONDS.sleep(3);
    UNSAFE.unpark(thread);
    System.out.println("SUCCESS!!!");
}
output==>
sub thread start
sub thread end
SUCCESS!!!
Process finished with exit code 0

unpark无法恢复处于sleep中的线程,只能与park配对使用,因为unpark发放的许可只有park能监听到

public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(() -> {
        try {
            System.err.println("sub thread start");
            TimeUnit.SECONDS.sleep(10);
            System.err.println("sub thread end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    thread.start();
    TimeUnit.SECONDS.sleep(3);
    UNSAFE.unpark(thread);
    System.out.println("SUCCESS!!!");
}

park和unpark的灵活之处

上面已经提到,unpark函数可以先于park调用,这个正是它们的灵活之处。

一个线程它有可能在别的线程unPark之前,或者之后,或者同时调用了park,那么因为park的特性,它可以不用担心自己的park的时序问题,否则,如果park必须要在unpark之前,那么给编程带来很大的麻烦!!

”考虑一下,两个线程同步,要如何处理?

在Java5里是用wait/notify/notifyAll来同步的。wait/notify机制有个很蛋疼的地方是,比如线程B要用notify通知线程A,那么线程B要确保线程A已经在wait调用上等待了,否则线程A可能永远都在等待。编程的时候就会很蛋疼。

另外,是调用notify,还是notifyAll?

notify只会唤醒一个线程,如果错误地有两个线程在同一个对象上wait等待,那么又悲剧了。为了安全起见,貌似只能调用notifyAll了“

park/unpark模型真正解耦了线程之间的同步,线程之间不再需要一个Object或者其它变量来存储状态,不再需要关心对方的状态

“Java Unsafe知识点有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. java Unsafe详细解析
  2. Java中有哪些Unsafe类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java unsafe

上一篇:怎么解决java并发请求下数据插入重复问题

下一篇:Django中的unittest应用是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》