`
dingchao.lonton
  • 浏览: 49704 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java unsafe 类源码

 
阅读更多
/** Unsafe.java - Unsafe operations needed for concurrency
   Copyright (C) 2006 Free Software Foundation
 
This file is part of GNU Classpath.
 
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
 
package sun.misc;
 
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.
 *
 * @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.
   *
   * @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.
   *
   * @param field the field whose offset should be returned.
   * @return the offset of the given 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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the integer field within <code>obj</code>.
   * @param expect the expected value of the field.
   * @param update the new value of the field if it equals <code>expect</code>.
   * @return true if the field was changed.
   */
  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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the long field within <code>obj</code>.
   * @param expect the expected value of the field.
   * @param update the new value of the field if it equals <code>expect</code>.
   * @return true if the field was changed.
   */
  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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the object field within <code>obj</code>.
   * @param expect the expected value of the field.
   * @param update the new value of the field if it equals <code>expect</code>.
   * @return true if the field was changed.
   */
  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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the integer field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the long field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the object field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the integer field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to read.
   * @param offset the offset of the integer field within <code>obj</code>.
   */
  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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the long field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the long field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to read.
   * @param offset the offset of the long field within <code>obj</code>.
   * @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.
   *
   * @param obj the object containing the field to read.
   * @param offset the offset of the long field within <code>obj</code>.
   * @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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the object field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to modify.
   * @param offset the offset of the object field within <code>obj</code>.
   * @param value the new value of the 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.
   *
   * @param obj the object containing the field to read.
   * @param offset the offset of the object field within <code>obj</code>.
   */
  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 that returned by
   * <a href="#arrayIndexScale"><code>arrayIndexScale</code></a>,
   * if non-zero.
   *
   * @param arrayClass the class for which the first element's address should
   *                   be obtained.
   * @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.
   *
   * @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.
   *
   * @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).
   *
   * @param isAbsolute true if the timeout is specified in milliseconds from
   *                   the epoch.
   * @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);
 
}
分享到:
评论

相关推荐

    JDK Unsafe 源码注释

    标题“JDK Unsafe 源码注释”和描述提示我们,接下来的内容将围绕JDK中非标准的Unsafe类进行。Unsafe类是Sun公司内部的一个类,它不是JDK标准API的一部分,而存在于sun.misc包中。虽然Oracle发行的JDK版本不包含...

    JDK8中sun.misc下UnSafe类源代码 UnSafe.java

    在Java编程中,sun.misc.UnSafe类是一个非常特殊的存在。这个类在JDK8中扮演着一个核心的角色,它提供了对Java语言规范中未公开的底层操作的访问。尽管UnSafe类并非设计为公共API的一部分,但它因其强大的功能而被...

    一篇看懂Java中的Unsafe类

    `Unsafe`类源码分析 `Unsafe`类包含大量本地方法,大致分为以下几个类别: 1. **Class相关**:提供对`Class`对象和静态字段的操作,如获取字段的内存偏移量。 2. **Object相关**:操作对象和其字段,如设置或...

    sun.misc.Unsafe源码

    在Java编程语言中,`sun.misc.Unsafe`类是一个神秘而强大的工具,它提供了对内存的直接操作和访问,绕过了Java的一些安全限制。这个类通常不被推荐在生产环境中直接使用,因为它的使用涉及到底层内存操作,可能会...

    JDK8中sun.misc包下的UnSafe类

    JDK8中sun.misc包下的UnSafe类,想查看源码的就拿走,没积分的请与我联系!xtfggef@gmail.com

    Java并发编程学习之Unsafe类与LockSupport类源码详析

    《深入解析Java并发编程:Unsafe类与LockSupport类源码剖析》 在Java并发编程领域,Unsafe类和LockSupport类是两个重要的底层工具类,它们提供了低级别的内存操作和线程控制,使得开发者能够实现高效的并发算法和...

    Java1.7源码包,包含sun

    在Java 1.7中,`sun`包下有许多关键的类,比如`java.lang.reflect包`中的`sun.misc.Unsafe`,这是一个强大的工具类,允许开发者访问和修改内存,执行低级别的操作,但同时也可能导致不安全的代码。 总的来说,这个...

    java 1.6 完整源码(包括 sun)

    1. **`sun.misc`**:这个包包含了各种实用工具和内部服务,如`Unsafe`类,它提供了对内存操作的低级访问,虽然不推荐使用,但在某些场景下可以实现高性能的编程。 2. **`sun.nio`**:包含了非阻塞I/O相关的类,如`...

    jdk源码-补充缺少sun包下的源码

    1. 提升性能:理解sun包下的源码,可以帮助优化程序性能,例如利用Unsafe类进行高效内存操作。 2. 深入理解JVM:通过阅读`sun.jvmstat`、`sun.hotspot`等包的源码,可以深入了解JVM的运行机制,如垃圾收集、性能监控...

    Jdk1.8源码,包含sun的源码

    它包含了JDK的核心类库和API的源代码,包括`sun`目录下的源码,这部分通常涉及Java的底层实现和一些非公开的API。在本讨论中,我们将详细探讨这个源码包中的关键知识点。 首先,`src/share/classes`目录是Java源码...

    jre 源码里面的sun. 开头源码

    1. **`sun.misc`**: 这个包包含了一些通用的实用工具类,如`Unsafe`类,它提供了对Java语言规范未明确规定的底层操作的访问,比如直接内存操作、字段偏移量获取等。这个类非常强大,但也很危险,因为它可以绕过一些...

    JDK1.6源码 JAVA

    JDK1.6的源码揭示了Java运行环境的实现,包括类加载机制、垃圾收集器、多线程支持等核心功能。例如,`java.lang.ClassLoader`类负责加载类文件,`java.lang.Thread`类实现了线程的创建和管理,而`java.lang.Runtime...

    jdk sun 开头的源码

    `sun.misc.Unsafe`类是一个特殊的存在,它提供了对Java内存模型的直接访问,尽管不建议在普通应用中使用,但在一些高性能或底层操作的场景下可能会用到。 5. **网络编程**:`sun.net`包包含了网络协议的实现,如TCP...

    java源码包(包括sun包和具体导入步骤)

    Java源码包是开发者深入理解Java语言和API工作原理的重要工具。这个压缩包包含了`sun`包和其他...通过Eclipse的源码浏览功能,我们可以方便地追踪方法调用,查看类的实现细节,从而加深对Java编程语言及其API的理解。

    免费开源-【Java学习+面试指南】部分内容大部分是Java程序员所需要掌握的核心知识

    Java序列化详解泛型&通配符详解Java 引用机制详解Java代理模式详解BigDecimal 详细解Java 魔法类 Unsafe 详细解Java SPI 机制详解Java语法糖详解集合知识点/面试题总结:Java集合常见知识点&面试题总结(上)(必看...

    jdk1.8的src,扩展了sun.reflect,unsafe

    通过查看源码,开发者可以了解这些包中的类和方法的具体实现,比如`java.util`包中的集合框架,`java.io`包的输入输出操作,以及`java.nio`包的非阻塞I/O等。 `jdi-overview.html`和`overview-core.html`、`...

    【Java学习+面试指南】 一份涵盖大部分Java程序员所需要掌握的核心知识

    Java 魔法类 Unsafe 详解 Java SPI 机制详解 Java 语法糖详解 集合 知识点/面试题总结: Java 集合常见知识点&面试题总结(上) (必看 ) Java 集合常见知识点&面试题总结(下) (必看 ) Java 容器使用注意事项总结 源码...

    JDK1.8源码,包含sun包,java包,org包等,完整的源码

    在JDK1.8中,`sun`包的源码可以让我们深入了解Java运行时的细节,例如`sun.misc.Unsafe`类,它是许多高级技术如内存操作和并发工具的基础。然而,需要注意的是,`sun`包中的类并不是Java标准API的一部分,可能在不同...

    JDK源码包(包含sun包下的源码)

    源码包是理解JDK内部工作原理的关键,它允许开发者深入学习和调试Java平台的底层机制。通常,JDK安装后的`src.zip`文件包含了大部分的Java标准库源代码,但这不包括`sun`包下的源码。`sun`包中的类是Oracle JDK特有...

    基于JDK源码解析Java领域中的并发锁之设计与实现.pdf

    它是基于 Unsafe 类实现的,可以实现非阻塞的线程挂起,提高并发效率。 三、Condition接口的设计与实现 Condition接口提供了比synchronized更细粒度的线程间通信机制,它允许线程等待特定条件,而不仅仅是等待锁的...

Global site tag (gtag.js) - Google Analytics