Unsafe

简单了解sun.misc.Unsafe 这里简单了解下sun.misc.Unsafe,jdk中的类很多地方都有用到它,它是许多类的实现的关键,知道这个类干嘛的,对一些源码的阅读很有帮助。 sun.misc.Unsafe算是Java留下的后门,能提供相当强大的操作,但是又不提供专业的文档。看名字就知道,sun本身就不推荐使用这个类。一般应用级的代码都不应该使用它,框架级别的,用这个的很多,比如jdk自身,一些框架netty,最常见的框架 阅读详情
//下面是sun.misc.Unsafe.java类源码
import java.lang.reflect.Field;

/***
 * This class should provide access to low-level operations and its
 * use should be limited to trusted code.  Fields can be accessed using
 * memory addresses, with undefined behaviour occurring if invalid memory
 * addresses are given.
 * 这个类提供了一个更底层的操作并且应该在受信任的代码中使用。可以通过内存地址
 * 存取fields,如果给出的内存地址是无效的那么会有一个不确定的运行表现。
 *
 * @author Tom Tromey (tromey@redhat.com)
 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
 */
public class Unsafe {
    // Singleton class.
    private static Unsafe unsafe = new Unsafe();

    /***
     * Private default constructor to prevent creation of an arbitrary
     * number of instances.
     * 使用私有默认构造器防止创建多个实例
     */
    private Unsafe() {
    }

    /***
     * Retrieve the singleton instance of <code>Unsafe</code>.  The calling
     * method should guard this instance from untrusted code, as it provides
     * access to low-level operations such as direct memory access.
     * 获取<code>Unsafe</code>的单例,这个方法调用应该防止在不可信的代码中实例,
     * 因为unsafe类提供了一个低级别的操作,例如直接内存存取。
     *
     * @throws SecurityException if a security manager exists and prevents
     *                           access to the system properties.
     *                           如果安全管理器不存在或者禁止访问系统属性
     */
    public static Unsafe getUnsafe() {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null)
            sm.checkPropertiesAccess();
        return unsafe;
    }

    /***
     * Returns the memory address offset of the given static field.
     * The offset is merely used as a means to access a particular field
     * in the other methods of this class.  The value is unique to the given
     * field and the same value should be returned on each subsequent call.
     * 返回指定静态field的内存地址偏移量,在这个类的其他方法中这个值只是被用作一个访问
     * 特定field的一个方式。这个值对于 给定的field是唯一的,并且后续对该方法的调用都应该
     * 返回相同的值。
     *
     * @param field the field whose offset should be returned.
     *              需要返回偏移量的field
     * @return the offset of the given field.
     *         指定field的偏移量
     */
    public native long objectFieldOffset(Field field);

    /***
     * Compares the value of the integer field at the specified offset
     * in the supplied object with the given expected value, and updates
     * it if they match.  The operation of this method should be atomic,
     * thus providing an uninterruptible way of updating an integer field.
     * 在obj的offset位置比较integer field和期望的值,如果相同则更新。这个方法
     * 的操作应该是原子的,因此提供了一种不可中断的方式更新integer field。
     *
     * @param obj the object containing the field to modify.
     *            包含要修改field的对象
     * @param offset the offset of the integer field within <code>obj</code>.
     *               <code>obj</code>中整型field的偏移量
     * @param expect the expected value of the field.
     *               希望field中存在的值
     * @param update the new value of the field if it equals <code>expect</code>.
     *           如果期望值expect与field的当前值相同,设置filed的值为这个新值
     * @return true if the field was changed.
     *                             如果field的值被更改
     */
    public native boolean compareAndSwapInt(Object obj, long offset,
                                            int expect, int update);

    /***
     * Compares the value of the long field at the specified offset
     * in the supplied object with the given expected value, and updates
     * it if they match.  The operation of this method should be atomic,
     * thus providing an uninterruptible way of updating a long field.
     * 在obj的offset位置比较long field和期望的值,如果相同则更新。这个方法
     * 的操作应该是原子的,因此提供了一种不可中断的方式更新long field。
     *
     * @param obj the object containing the field to modify.
     *              包含要修改field的对象 
     * @param offset the offset of the long field within <code>obj</code>.
     *               <code>obj</code>中long型field的偏移量
     * @param expect the expected value of the field.
     *               希望field中存在的值
     * @param update the new value of the field if it equals <code>expect</code>.
     *               如果期望值expect与field的当前值相同,设置filed的值为这个新值
     * @return true if the field was changed.
     *              如果field的值被更改
     */
    public native boolean compareAndSwapLong(Object obj, long offset,
                                             long expect, long update);

    /***
     * Compares the value of the object field at the specified offset
     * in the supplied object with the given expected value, and updates
     * it if they match.  The operation of this method should be atomic,
     * thus providing an uninterruptible way of updating an object field.
     * 在obj的offset位置比较object field和期望的值,如果相同则更新。这个方法
     * 的操作应该是原子的,因此提供了一种不可中断的方式更新object field。
     *
     * @param obj the object containing the field to modify.
     *    包含要修改field的对象 
     * @param offset the offset of the object field within <code>obj</code>.
     *         <code>obj</code>中object型field的偏移量
     * @param expect the expected value of the field.
     *               希望field中存在的值
     * @param update the new value of the field if it equals <code>expect</code>.
     *               如果期望值expect与field的当前值相同,设置filed的值为这个新值
     * @return true if the field was changed.
     *              如果field的值被更改
     */
    public native boolean compareAndSwapObject(Object obj, long offset,
                                               Object expect, Object update);

    /***
     * Sets the value of the integer field at the specified offset in the
     * supplied object to the given value.  This is an ordered or lazy
     * version of <code>putIntVolatile(Object,long,int)</code>, which
     * doesn't guarantee the immediate visibility of the change to other
     * threads.  It is only really useful where the integer field is
     * <code>volatile</code>, and is thus expected to change unexpectedly.
     * 设置obj对象中offset偏移地址对应的整型field的值为指定值。这是一个有序或者
     * 有延迟的<code>putIntVolatile</cdoe>方法,并且不保证值的改变被其他线程立
     * 即看到。只有在field被<code>volatile</code>修饰并且期望被意外修改的时候
     * 使用才有用。
     *
     * @param obj the object containing the field to modify.
     *    包含需要修改field的对象
     * @param offset the offset of the integer field within <code>obj</code>.
     *       <code>obj</code>中整型field的偏移量
     * @param value the new value of the field.
     *      field将被设置的新值
     * @see #putIntVolatile(Object, long, int)
     */
    public native void putOrderedInt(Object obj, long offset, int value);

    /***
     * Sets the value of the long field at the specified offset in the
     * supplied object to the given value.  This is an ordered or lazy
     * version of <code>putLongVolatile(Object,long,long)</code>, which
     * doesn't guarantee the immediate visibility of the change to other
     * threads.  It is only really useful where the long field is
     * <code>volatile</code>, and is thus expected to change unexpectedly.
     * 设置obj对象中offset偏移地址对应的long型field的值为指定值。这是一个有序或者
     * 有延迟的<code>putLongVolatile</cdoe>方法,并且不保证值的改变被其他线程立
     * 即看到。只有在field被<code>volatile</code>修饰并且期望被意外修改的时候
     * 使用才有用。
     *
     * @param obj the object containing the field to modify.
     *    包含需要修改field的对象
     * @param offset the offset of the long field within <code>obj</code>.
     *       <code>obj</code>中long型field的偏移量
     * @param value the new value of the field.
     *      field将被设置的新值
     * @see #putLongVolatile(Object, long, long)
     */
    public native void putOrderedLong(Object obj, long offset, long value);

    /***
     * Sets the value of the object field at the specified offset in the
     * supplied object to the given value.  This is an ordered or lazy
     * version of <code>putObjectVolatile(Object,long,Object)</code>, which
     * doesn't guarantee the immediate visibility of the change to other
     * threads.  It is only really useful where the object field is
     * <code>volatile</code>, and is thus expected to change unexpectedly.
     * 设置obj对象中offset偏移地址对应的object型field的值为指定值。这是一个有序或者
     * 有延迟的<code>putObjectVolatile</cdoe>方法,并且不保证值的改变被其他线程立
     * 即看到。只有在field被<code>volatile</code>修饰并且期望被意外修改的时候
     * 使用才有用。
     *
     * @param obj the object containing the field to modify.
     *    包含需要修改field的对象
     * @param offset the offset of the object field within <code>obj</code>.
     *       <code>obj</code>中long型field的偏移量
     * @param value the new value of the field.
     *      field将被设置的新值
     */
    public native void putOrderedObject(Object obj, long offset, Object value);

    /***
     * Sets the value of the integer field at the specified offset in the
     * supplied object to the given value, with volatile store semantics.
     * 设置obj对象中offset偏移地址对应的整型field的值为指定值。支持volatile store语义
     *
     * @param obj the object containing the field to modify.
     *    包含需要修改field的对象
     * @param offset the offset of the integer field within <code>obj</code>.
     *       <code>obj</code>中整型field的偏移量
     * @param value the new value of the field.
     *       field将被设置的新值
     */
    public native void putIntVolatile(Object obj, long offset, int value);

    /***
     * Retrieves the value of the integer field at the specified offset in the
     * supplied object with volatile load semantics.
     * 获取obj对象中offset偏移地址对应的整型field的值,支持volatile load语义。
     *
     * @param obj the object containing the field to read.
     *    包含需要去读取的field的对象
     * @param offset the offset of the integer field within <code>obj</code>.
     *       <code>obj</code>中整型field的偏移量
     */
    public native int getIntVolatile(Object obj, long offset);

    /***
     * Sets the value of the long field at the specified offset in the
     * supplied object to the given value, with volatile store semantics.
     * 设置obj对象中offset偏移地址对应的long型field的值为指定值。支持volatile store语义
     *
     * @param obj the object containing the field to modify.
     *            包含需要修改field的对象
     * @param offset the offset of the long field within <code>obj</code>.
     *               <code>obj</code>中long型field的偏移量
     * @param value the new value of the field.
     *              field将被设置的新值
     * @see #putLong(Object, long, long)
     */
    public native void putLongVolatile(Object obj, long offset, long value);

    /***
     * Sets the value of the long field at the specified offset in the
     * supplied object to the given value.
     * 设置obj对象中offset偏移地址对应的long型field的值为指定值。
     *
     * @param obj the object containing the field to modify.
     *     包含需要修改field的对象
     * @param offset the offset of the long field within <code>obj</code>.
     *     <code>obj</code>中long型field的偏移量
     * @param value the new value of the field.
     *     field将被设置的新值
     * @see #putLongVolatile(Object, long, long)
     */
    public native void putLong(Object obj, long offset, long value);

    /***
     * Retrieves the value of the long field at the specified offset in the
     * supplied object with volatile load semantics.
     * 获取obj对象中offset偏移地址对应的long型field的值,支持volatile load语义。
     *
     * @param obj the object containing the field to read.
     *    包含需要去读取的field的对象
     * @param offset the offset of the long field within <code>obj</code>.
     *       <code>obj</code>中long型field的偏移量
     * @see #getLong(Object, long)
     */
    public native long getLongVolatile(Object obj, long offset);

    /***
     * Retrieves the value of the long field at the specified offset in the
     * supplied object.
     * 获取obj对象中offset偏移地址对应的long型field的值
     *
     * @param obj the object containing the field to read.
     *    包含需要去读取的field的对象
     * @param offset the offset of the long field within <code>obj</code>.
     *       <code>obj</code>中long型field的偏移量
     * @see #getLongVolatile(Object, long)
     */
    public native long getLong(Object obj, long offset);

    /***
     * Sets the value of the object field at the specified offset in the
     * supplied object to the given value, with volatile store semantics.
     * 设置obj对象中offset偏移地址对应的object型field的值为指定值。支持volatile store语义
     *
     * @param obj the object containing the field to modify.
     *    包含需要修改field的对象
     * @param offset the offset of the object field within <code>obj</code>.
     *     <code>obj</code>中object型field的偏移量
     * @param value the new value of the field.
     *       field将被设置的新值
     * @see #putObject(Object, long, Object)
     */
    public native void putObjectVolatile(Object obj, long offset, Object value);

    /***
     * Sets the value of the object field at the specified offset in the
     * supplied object to the given value.
     * 设置obj对象中offset偏移地址对应的object型field的值为指定值。
     *
     * @param obj the object containing the field to modify.
     *    包含需要修改field的对象
     * @param offset the offset of the object field within <code>obj</code>.
     *     <code>obj</code>中object型field的偏移量
     * @param value the new value of the field.
     *       field将被设置的新值
     * @see #putObjectVolatile(Object, long, Object)
     */
    public native void putObject(Object obj, long offset, Object value);

    /***
     * Retrieves the value of the object field at the specified offset in the
     * supplied object with volatile load semantics.
     * 获取obj对象中offset偏移地址对应的object型field的值,支持volatile load语义。
     *
     * @param obj the object containing the field to read.
     *    包含需要去读取的field的对象
     * @param offset the offset of the object field within <code>obj</code>.
     *       <code>obj</code>中object型field的偏移量
     */
    public native Object getObjectVolatile(Object obj, long offset);

    /***
     * Returns the offset of the first element for a given array class.
     * To access elements of the array class, this value may be used along with
     * with that returned by 
     * <a href="#arrayIndexScale"><code>arrayIndexScale</code></a>,
     * if non-zero.
     * 获取给定数组中第一个元素的偏移地址。
     * 为了存取数组中的元素,这个偏移地址与<a href="#arrayIndexScale"><code>arrayIndexScale
     * </code></a>方法的非0返回值一起被使用。
     * @param arrayClass the class for which the first element's address should
     *                   be obtained.
     *                   第一个元素地址被获取的class
     * @return the offset of the first element of the array class.
     *    数组第一个元素 的偏移地址
     * @see arrayIndexScale(Class)
     */
    public native int arrayBaseOffset(Class arrayClass);

    /***
     * Returns the scale factor used for addressing elements of the supplied
     * array class.  Where a suitable scale factor can not be returned (e.g.
     * for primitive types), zero should be returned.  The returned value
     * can be used with 
     * <a href="#arrayBaseOffset"><code>arrayBaseOffset</code></a>
     * to access elements of the class.
     * 获取用户给定数组寻址的换算因子.一个合适的换算因子不能返回的时候(例如:基本类型),
     * 返回0.这个返回值能够与<a href="#arrayBaseOffset"><code>arrayBaseOffset</code>
     * </a>一起使用去存取这个数组class中的元素
     *
     * @param arrayClass the class whose scale factor should be returned.
     * @return the scale factor, or zero if not supported for this array class.
     */
    public native int arrayIndexScale(Class arrayClass);

    /***
     * Releases the block on a thread created by 
     * <a href="#park"><code>park</code></a>.  This method can also be used
     * to terminate a blockage caused by a prior call to <code>park</code>.
     * This operation is unsafe, as the thread must be guaranteed to be
     * live.  This is true of Java, but not native code.
     * 释放被<a href="#park"><code>park</code></a>创建的在一个线程上的阻塞.这个
     * 方法也可以被使用来终止一个先前调用<code>park</code>导致的阻塞.
     * 这个操作操作时不安全的,因此线程必须保证是活的.这是java代码不是native代码。
     * @param thread the thread to unblock.
     *           要解除阻塞的线程
     */
    public native void unpark(Thread thread);

    /***
     * Blocks the thread until a matching 
     * <a href="#unpark"><code>unpark</code></a> occurs, the thread is
     * interrupted or the optional timeout expires.  If an <code>unpark</code>
     * call has already occurred, this also counts.  A timeout value of zero
     * is defined as no timeout.  When <code>isAbsolute</code> is
     * <code>true</code>, the timeout is in milliseconds relative to the
     * epoch.  Otherwise, the value is the number of nanoseconds which must
     * occur before timeout.  This call may also return spuriously (i.e.
     * for no apparent reason).
     * 阻塞一个线程直到<a href="#unpark"><code>unpark</code></a>出现、线程
     * 被中断或者timeout时间到期。如果一个<code>unpark</code>调用已经出现了,
     * 这里只计数。timeout为0表示永不过期.当<code>isAbsolute</code>为true时,
     * timeout是相对于新纪元之后的毫秒。否则这个值就是超时前的纳秒数。这个方法执行时
     * 也可能不合理地返回(没有具体原因)
     *
     * @param isAbsolute true if the timeout is specified in milliseconds from
     *                   the epoch.
     *                   如果为true timeout的值是一个相对于新纪元之后的毫秒数
     * @param time either the number of nanoseconds to wait, or a time in
     *             milliseconds from the epoch to wait for.
     *             可以是一个要等待的纳秒数,或者是一个相对于新纪元之后的毫秒数直到
     *             到达这个时间点
     */
    public native void park(boolean isAbsolute, long time);
}
又一次Java线程卡死的调试经历 我的java代码,每天凌晨1点都会执行一个定时任务,定时任务中有循环执行的任务,每次循环都有开始和结束日志,某一天,我忽然发现,日志中前一天的循环只执行了几次就停止了,根据日志来看,执行到第4次循环的时候,只有开始的日志,没有结束的日志,那肯定是在里面卡死了 我首先执行 # ps -ef | grep tomcat 获取到了线程的pid 然后执行jstack # jstack -l 1... 阅读详情

相关推荐

Java中的魔法类:sun.misc.Unsafe示例详解

Java是一个安全的开发工具,它阻止开发人员犯很多低级的错误,而大部份的错误都是基于内存管理方面的。如果你想搞破坏,可以使用Unsafe这个类。下面这篇文章主要给大家介绍了关于Java中魔法类:sun.misc.Unsafe的相关资料,需要的朋友可以参考下

Java报错sun.misc.Unsafe.park(Native Method)

这个问题基本上是Spring在启动的时候,某个东西重复被初始化(请仔细检查自己有没有重名的接口,我好几次都是接口重名导致的),导致内存溢出或者其它。我们在编程的过程中,一定要细心,避免类似的错误发生。 ...

weixin_37999518的博客 1万+

告别第三方工具!用Windows自带蓝牙玩转BLE设备调试(保姆级图文教程)

本文详细介绍了如何利用Windows自带的蓝牙功能进行BLE设备调试,无需第三方工具。从硬件准备、系统配置到实战操作,提供保姆级图文教程,帮助开发者高效完成设备发现、数据交互等全流程调试工作。特别适合物联网开发者和智能硬件爱好者。

o5p6q的博客 725

sun.misc.Unsafe.park(Native Method)异常

这里写自定义目录标题sun.misc.Unsafe.park(Native Method)异常,异常如下: sun.misc.Unsafe.park(Native Method)异常,异常如下: 2019/07/30-09:40:34 [localhost-startStop-1] WARN org.apache.catalina.loader.WebappClassLoaderBase - T...

weixin_41160551的博客 8万+

URScript介绍

优傲机器人(Universal Robots)是丹麦工业协作机器人制造商,2005年成立,2015年被泰瑞达收购。其自研编程语言URScript基于Python语法,专为机器人控制优化,支持运动指令、IO控制等功能,具有类Python的简洁语法和实时性特点。URScript提供丰富的API,支持变量赋值、条件判断、循环及函数定义等编程结构,参数按值传递。用户可通过该语言在Polyscope界面编写程序,实现自动化任务。目前优傲在全球20多个办事处为多国客户提供服务。

谢谢大家的关注和点赞!这里只有纯纯的知识干货,没有一句废话。希望能实实在在帮到大家~要是觉得有用,别忘了给我点支持哟,你 602

YOLOv11涨点改进| SCI一区2026 |独家创新首发、特征融合改进篇|引入FAFM特征对齐融合模块,通过特征对齐和融合两个阶段,助力多模态融合小目标检测,小目标检测,SAR图像目标检测高效涨点

🔥本文提出了一种改进YOLOv11网络模型的方法,通过引入FAFM(特征对齐融合模块)来解决多模态目标检测中的特征空间错位问题。FAFM模块采用可变形卷积和内容引导注意力机制,有效实现了跨模态特征对齐与融合,充分利用了不同模态间的互补信息。该方法在保持模型轻量化的同时,显著提升了目标检测精度和鲁棒性,特别是在复杂背景和模态差异较大的场景下表现突出。文章详细介绍了FAFM模块的结构、原理、优势,并提供了完整的代码实现和模型配置方案,为多模态目标检测任务提供了实用的技术参考。

Ai缝合怪、助力小伙伴高效发论文 267

Unsafe 详解

学习JUC的过程中发现有一个类很神奇,今天我们就来一起学习一下Unsafe类。

qq_73210658的博客 2819

【CanMV K210】AI 视觉 MNIST 手写数字识别与分类推理

本文介绍了基于CanMV K210开发板的MNIST手写数字识别实验。实验通过摄像头采集图像,经过开窗、灰度化、缩放、反色等预处理后,使用KPU神经网络加速器运行MNIST分类模型,最终在LCD上显示识别结果。重点讲解了分类模型与检测模型的区别、图像预处理流程以及KPU推理结果的解析方法。实验帮助理解从图像采集到分类输出的完整AI视觉处理链路,并提供了调试思路和硬件连接说明。

Mr数据杨 364

Unsafe类

Unsafe类

蔡林浩的博客 1119

软考高级信息系统项目管理师系列论文之:百篇软考高级信息系统项目管理师论文范文

软考高级信息系统项目管理师系列论文之:百篇软考高级信息系统项目管理师论文范文

zhengzaifeidelushang的博客 3578

Qt入门教程【基础控件篇】QLineEdit输入框

QLineEdit输入框

小鱼酱的博客 3260

【Rust自学】19.1. 摆脱安全性限制的unsafe Rust

到目前为止我们讨论的所有代码都在编译时强制执行Rust的内存安全保证。然而,Rust内部隐藏着第二种语言,它不强制执行这些内存安全保证,被称为unsafe Rust。它和普通Rust一样,但给了我们额外的“超能力”。

A Rookie in San Domenico School 3156

Xiaojie雷达之路---手把手教你从串口获取雷达原始数据(一)---新建工程(开始)

Hello,大家好,我是Xiaojie,欢迎大家能够和Xiaojie来一起学习毫米波雷达知识,Xiaojie准备连载一个系列的文章---利用串口获取毫米波雷达中的原始数据,本系列文章将从新建工程到最后原始数据显示到PC上进行逐步介绍,这个系列的文章大约是一个4-5篇左右。本篇文章呢,主要讲的是如何新建一个毫米波雷达的工程。

XiaoJie的博客 5808

Rust unsafe

编译器无法自动验证这段代码的内存安全性程序员需要手动确保代码符合Rust 的安全约定允许访问一些通常被禁止的低级功能。

weixin_40398522的博客 557

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【Rust】unsafe rust入门

C++中其实没有unsafe这个东西,像类似裸指针这种,在C++中甚至是一种比较常用的用法。毕竟智能指针,比如shared_ptrunique_ptr,用法更为复杂。所以我个人认为,Rust的unsafe的意义是,将这些不安全的操作变得复杂,变得难写,进而引导程序员选择更加简单,更加好写的安全用法。这和C++如今的处境刚好相反,C++中按照安全原则写出来的代码都比较复杂,这也是历史原因,毕竟不能动现成的代码。另外,unsafe也是一种承诺,不再由编译器保证代码的安全性,而是由程序员自己来保证。

矢车菊二十七号 1696

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Rust unsafe 入门:什么时候必须用 unsafe,以及怎么把它包安全

unsafe 只放开五件事:裸指针解引用、调用 unsafe 函数、访问可变静态变量、实现 unsafe trait、访问 union 字段。其余 Rust 安全检查照常工作。四个绕不开的 unsafe 场景:FFI 调用 C 库、高性能自定义数据结构、硬件/内存映射 I/O、极少见的借用规则突破。除此之外,绝大多数场景都不需要 unsafe。四条包装铁律:unsafe 块尽量小、安全前提必须可证明、用类型系统把 unsafe 关在笼子里、不确定就别用。

no1coder的博客 7924

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

实战:JUC系列之基础类Unsafe应用场景【阻塞队列 Unsafe.park/Unsafe.unpark】

《JUC系列之Unsafe类详解》介绍了Java底层类sun.misc.Unsafe的功能与实现原理。文章通过一个支持超时的阻塞队列案例,展示了Unsafe在并发编程中的典型应用:1)使用CAS原子操作(compareAndSwapInt)实现线程安全;2)通过park/unpark方法控制线程阻塞与唤醒。特别强调Unsafe作为内部API存在风险,建议优先使用java.util.concurrent包的标准工具。该技术博客深入解析了Unsafe的内存操作、线程控制等底层机制,为理解Java并发原理提供了重

为无为,事无事,味无味。 1107

手撕Java系列--Unsafe详解,Unsafe特点、Unsafe常用方法、Unsafe对Java并发的支持、Unsafe在其他领域(JVM、反射、动态代理、序列化、NIO、Guava)的应用

Unsafe类通过提供底层的内存操作和原子操作,使得Java并发工具能够高效地实现同步和并发控制。虽然Unsafe不是官方推荐的API,但在Java并发编程中发挥了重要作用。使用这些并发工具时,开发者通常不需要直接接触Unsafe,因为这些工具已经封装好了底层的复杂操作。

横竖撇折点 1311

Unsafe总结

前几天下班回家的路上一个客户端同事问我,java能自己控制内存吗?再加上这几天在看disruptor源码,也遇到了Unsafe相关的东西,抽个时间总结一下。 Unsafe类,下面来介绍一下这个类 Unsafe类是final类,并且构造方法是私有的 public final class Unsafe { private static native void registerNatives(); static { registerNatives(); }

ayupch的博客 316

Unsafe类详解

Unsafe类、CAS

CodePanda@GPF的博客 2149

Java Unsafe and volatile

起源在阅读ConcurrentLinkedQueue中看到了很奇怪的用法,难以理解,如下,为什么会用到UNSAFE的这个putOrderedObject方法呢/** * Tries to CAS head to p. If successful, repoint old head to itself * as sentinel for succ(), below. *...

心中永远的正能量 2214

JAVA之Unsafe学习笔记

sun.misc.Unsafe作用:可以用来在任意内存地址位置处读写数据,支持一些CAS原子操作 Java最初被设计为一种安全的受控环境。尽管如此,HotSpot还是包含了一个后门sun.misc.Unsafe,提供了一些可以直接操控内存和线程的底层操作。Unsafe被JDK广泛应用于java.nio和并发包等实现中,这个不安全的类提供了一个观察HotSpot JVM内部结构并且可以对其进行修改,

luoyoub的博客 4775

如何使用Unsafe类方法

首先Unsafe类是不建议被使用的,因为他面向底层,可能在每一代jdk版本中发生变化,除非你有把握在在每一次升级jdk时维护你的项目 Unsafe是作为单例而存在的,当我们尝试调用getUnsafe方法时,会报安全错误,这是由于双亲加载机制导致的。通常我们可以通过反射来绕过这些检测 在如下代码中,我们通过反射获取到了Unsafe类的实例,Unsafe类中的方法往往都是通过偏移量来操作对象的,我们...

qq_39835588的博客 1149
上一篇: spring文件上传
下一篇: RPC
wugy111
博客等级 码龄14年 8粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值