hadoop中使用的Unsafe.java
今天查看hadoop源代码, 发现有个Unsafe.java
稍微总结下
优势 1 减少线程调度开销, Unsafe.java 通过采用非堵塞原子方式来减少线程调度开销
2 传统线程通信通过wait,notify方法实现(会有信号量的堵塞队列),而Unsafe使用操作系统调度命令park,unpark, 减少信号量的开销,提高新能。
差不多和windows下的InterlockedDecrement差不多的概念, 只是Unsafe的功能更加强大,可以获取的类型更多,另外可以有C/C++方式的内存分配模式以及内存交换等更多操作
Java中不直接使用Unsafe.java,封装成原子类给用户使用
public class AtomicInteger extends Number implements java.io.Serializable {
private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
其他还有很多类型.
附源代码
/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package sun.misc; import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import sun.reflect.CallerSensitive;
import sun.reflect.Reflection; public final class Unsafe
{
private static final Unsafe theUnsafe;
public static final int INVALID_FIELD_OFFSET = -1;
public static final int ARRAY_BOOLEAN_BASE_OFFSET;
public static final int ARRAY_BYTE_BASE_OFFSET;
public static final int ARRAY_SHORT_BASE_OFFSET;
public static final int ARRAY_CHAR_BASE_OFFSET;
public static final int ARRAY_INT_BASE_OFFSET;
public static final int ARRAY_LONG_BASE_OFFSET;
public static final int ARRAY_FLOAT_BASE_OFFSET;
public static final int ARRAY_DOUBLE_BASE_OFFSET;
public static final int ARRAY_OBJECT_BASE_OFFSET;
public static final int ARRAY_BOOLEAN_INDEX_SCALE;
public static final int ARRAY_BYTE_INDEX_SCALE;
public static final int ARRAY_SHORT_INDEX_SCALE;
public static final int ARRAY_CHAR_INDEX_SCALE;
public static final int ARRAY_INT_INDEX_SCALE;
public static final int ARRAY_LONG_INDEX_SCALE;
public static final int ARRAY_FLOAT_INDEX_SCALE;
public static final int ARRAY_DOUBLE_INDEX_SCALE;
public static final int ARRAY_OBJECT_INDEX_SCALE;
public static final int ADDRESS_SIZE; private static native void registerNatives(); @CallerSensitive
public static Unsafe getUnsafe()
{
Class localClass = Reflection.getCallerClass();
if (localClass.getClassLoader() != null)
throw new SecurityException("Unsafe");
return theUnsafe;
} public native int getInt(Object paramObject, long paramLong); public native void putInt(Object paramObject, long paramLong, int paramInt); public native Object getObject(Object paramObject, long paramLong); public native void putObject(Object paramObject1, long paramLong, Object paramObject2); public native boolean getBoolean(Object paramObject, long paramLong); public native void putBoolean(Object paramObject, long paramLong, boolean paramBoolean); public native byte getByte(Object paramObject, long paramLong); public native void putByte(Object paramObject, long paramLong, byte paramByte); public native short getShort(Object paramObject, long paramLong); public native void putShort(Object paramObject, long paramLong, short paramShort); public native char getChar(Object paramObject, long paramLong); public native void putChar(Object paramObject, long paramLong, char paramChar); public native long getLong(Object paramObject, long paramLong); public native void putLong(Object paramObject, long paramLong1, long paramLong2); public native float getFloat(Object paramObject, long paramLong); public native void putFloat(Object paramObject, long paramLong, float paramFloat); public native double getDouble(Object paramObject, long paramLong); public native void putDouble(Object paramObject, long paramLong, double paramDouble); @Deprecated
public int getInt(Object paramObject, int paramInt)
{
return getInt(paramObject, paramInt);
} @Deprecated
public void putInt(Object paramObject, int paramInt1, int paramInt2)
{
putInt(paramObject, paramInt1, paramInt2);
} @Deprecated
public Object getObject(Object paramObject, int paramInt)
{
return getObject(paramObject, paramInt);
} @Deprecated
public void putObject(Object paramObject1, int paramInt, Object paramObject2)
{
putObject(paramObject1, paramInt, paramObject2);
} @Deprecated
public boolean getBoolean(Object paramObject, int paramInt)
{
return getBoolean(paramObject, paramInt);
} @Deprecated
public void putBoolean(Object paramObject, int paramInt, boolean paramBoolean)
{
putBoolean(paramObject, paramInt, paramBoolean);
} @Deprecated
public byte getByte(Object paramObject, int paramInt)
{
return getByte(paramObject, paramInt);
} @Deprecated
public void putByte(Object paramObject, int paramInt, byte paramByte)
{
putByte(paramObject, paramInt, paramByte);
} @Deprecated
public short getShort(Object paramObject, int paramInt)
{
return getShort(paramObject, paramInt);
} @Deprecated
public void putShort(Object paramObject, int paramInt, short paramShort)
{
putShort(paramObject, paramInt, paramShort);
} @Deprecated
public char getChar(Object paramObject, int paramInt)
{
return getChar(paramObject, paramInt);
} @Deprecated
public void putChar(Object paramObject, int paramInt, char paramChar)
{
putChar(paramObject, paramInt, paramChar);
} @Deprecated
public long getLong(Object paramObject, int paramInt)
{
return getLong(paramObject, paramInt);
} @Deprecated
public void putLong(Object paramObject, int paramInt, long paramLong)
{
putLong(paramObject, paramInt, paramLong);
} @Deprecated
public float getFloat(Object paramObject, int paramInt)
{
return getFloat(paramObject, paramInt);
} @Deprecated
public void putFloat(Object paramObject, int paramInt, float paramFloat)
{
putFloat(paramObject, paramInt, paramFloat);
} @Deprecated
public double getDouble(Object paramObject, int paramInt)
{
return getDouble(paramObject, paramInt);
} @Deprecated
public void putDouble(Object paramObject, int paramInt, double paramDouble)
{
putDouble(paramObject, paramInt, paramDouble);
} public native byte getByte(long paramLong); public native void putByte(long paramLong, byte paramByte); public native short getShort(long paramLong); public native void putShort(long paramLong, short paramShort); public native char getChar(long paramLong); public native void putChar(long paramLong, char paramChar); public native int getInt(long paramLong); public native void putInt(long paramLong, int paramInt); public native long getLong(long paramLong); public native void putLong(long paramLong1, long paramLong2); public native float getFloat(long paramLong); public native void putFloat(long paramLong, float paramFloat); public native double getDouble(long paramLong); public native void putDouble(long paramLong, double paramDouble); public native long getAddress(long paramLong); public native void putAddress(long paramLong1, long paramLong2); public native long allocateMemory(long paramLong); public native long reallocateMemory(long paramLong1, long paramLong2); public native void setMemory(Object paramObject, long paramLong1, long paramLong2, byte paramByte); public void setMemory(long paramLong1, long paramLong2, byte paramByte)
{
setMemory(null, paramLong1, paramLong2, paramByte);
} public native void copyMemory(Object paramObject1, long paramLong1, Object paramObject2, long paramLong2, long paramLong3); public void copyMemory(long paramLong1, long paramLong2, long paramLong3)
{
copyMemory(null, paramLong1, null, paramLong2, paramLong3);
} public native void freeMemory(long paramLong); @Deprecated
public int fieldOffset(Field paramField)
{
if (Modifier.isStatic(paramField.getModifiers())) {
return (int)staticFieldOffset(paramField);
}
return (int)objectFieldOffset(paramField);
} @Deprecated
public Object staticFieldBase(Class paramClass)
{
Field[] arrayOfField = paramClass.getDeclaredFields();
for (int i = 0; i < arrayOfField.length; ++i) {
if (Modifier.isStatic(arrayOfField[i].getModifiers())) {
return staticFieldBase(arrayOfField[i]);
}
}
return null;
} public native long staticFieldOffset(Field paramField); public native long objectFieldOffset(Field paramField); public native Object staticFieldBase(Field paramField); public native boolean shouldBeInitialized(Class<?> paramClass); public native void ensureClassInitialized(Class paramClass); public native int arrayBaseOffset(Class paramClass); public native int arrayIndexScale(Class paramClass); public native int addressSize(); public native int pageSize(); public native Class defineClass(String paramString, byte[] paramArrayOfByte, int paramInt1, int paramInt2, ClassLoader paramClassLoader, ProtectionDomain paramProtectionDomain); @Deprecated
@CallerSensitive
public native Class defineClass(String paramString, byte[] paramArrayOfByte, int paramInt1, int paramInt2); public native Class defineAnonymousClass(Class paramClass, byte[] paramArrayOfByte, Object[] paramArrayOfObject); public native Object allocateInstance(Class paramClass)
throws InstantiationException; public native void monitorEnter(Object paramObject); public native void monitorExit(Object paramObject); public native boolean tryMonitorEnter(Object paramObject); public native void throwException(Throwable paramThrowable); public final native boolean compareAndSwapObject(Object paramObject1, long paramLong, Object paramObject2, Object paramObject3); public final native boolean compareAndSwapInt(Object paramObject, long paramLong, int paramInt1, int paramInt2); public final native boolean compareAndSwapLong(Object paramObject, long paramLong1, long paramLong2, long paramLong3); public native Object getObjectVolatile(Object paramObject, long paramLong); public native void putObjectVolatile(Object paramObject1, long paramLong, Object paramObject2); public native int getIntVolatile(Object paramObject, long paramLong); public native void putIntVolatile(Object paramObject, long paramLong, int paramInt); public native boolean getBooleanVolatile(Object paramObject, long paramLong); public native void putBooleanVolatile(Object paramObject, long paramLong, boolean paramBoolean); public native byte getByteVolatile(Object paramObject, long paramLong); public native void putByteVolatile(Object paramObject, long paramLong, byte paramByte); public native short getShortVolatile(Object paramObject, long paramLong); public native void putShortVolatile(Object paramObject, long paramLong, short paramShort); public native char getCharVolatile(Object paramObject, long paramLong); public native void putCharVolatile(Object paramObject, long paramLong, char paramChar); public native long getLongVolatile(Object paramObject, long paramLong); public native void putLongVolatile(Object paramObject, long paramLong1, long paramLong2); public native float getFloatVolatile(Object paramObject, long paramLong); public native void putFloatVolatile(Object paramObject, long paramLong, float paramFloat); public native double getDoubleVolatile(Object paramObject, long paramLong); public native void putDoubleVolatile(Object paramObject, long paramLong, double paramDouble); public native void putOrderedObject(Object paramObject1, long paramLong, Object paramObject2); public native void putOrderedInt(Object paramObject, long paramLong, int paramInt); public native void putOrderedLong(Object paramObject, long paramLong1, long paramLong2); public native void unpark(Object paramObject); public native void park(boolean paramBoolean, long paramLong); public native int getLoadAverage(double[] paramArrayOfDouble, int paramInt); static
{
registerNatives();
Reflection.registerMethodsToFilter(Unsafe.class, new String[] { "getUnsafe" }); theUnsafe = new Unsafe(); ARRAY_BOOLEAN_BASE_OFFSET = theUnsafe.arrayBaseOffset([Z.class); ARRAY_BYTE_BASE_OFFSET = theUnsafe.arrayBaseOffset([B.class); ARRAY_SHORT_BASE_OFFSET = theUnsafe.arrayBaseOffset([S.class); ARRAY_CHAR_BASE_OFFSET = theUnsafe.arrayBaseOffset([C.class); ARRAY_INT_BASE_OFFSET = theUnsafe.arrayBaseOffset([I.class); ARRAY_LONG_BASE_OFFSET = theUnsafe.arrayBaseOffset([J.class); ARRAY_FLOAT_BASE_OFFSET = theUnsafe.arrayBaseOffset([F.class); ARRAY_DOUBLE_BASE_OFFSET = theUnsafe.arrayBaseOffset([D.class); ARRAY_OBJECT_BASE_OFFSET = theUnsafe.arrayBaseOffset([Ljava.lang.Object.class); ARRAY_BOOLEAN_INDEX_SCALE = theUnsafe.arrayIndexScale([Z.class); ARRAY_BYTE_INDEX_SCALE = theUnsafe.arrayIndexScale([B.class); ARRAY_SHORT_INDEX_SCALE = theUnsafe.arrayIndexScale([S.class); ARRAY_CHAR_INDEX_SCALE = theUnsafe.arrayIndexScale([C.class); ARRAY_INT_INDEX_SCALE = theUnsafe.arrayIndexScale([I.class); ARRAY_LONG_INDEX_SCALE = theUnsafe.arrayIndexScale([J.class); ARRAY_FLOAT_INDEX_SCALE = theUnsafe.arrayIndexScale([F.class); ARRAY_DOUBLE_INDEX_SCALE = theUnsafe.arrayIndexScale([D.class); ARRAY_OBJECT_INDEX_SCALE = theUnsafe.arrayIndexScale([Ljava.lang.Object.class); ADDRESS_SIZE = theUnsafe.addressSize();
}
}
hadoop中使用的Unsafe.java的更多相关文章
- hadoop中Text类 与 java中String类的区别
hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...
- Hadoop中java.lang.ClassCastException: partition解决方法
java.lang.ClassCastException: partition.KpiWritable cannot be cast to org.apache.hadoop.io.LongWrita ...
- hadoop中实现java网络爬虫
这一篇网络爬虫的实现就要联系上大数据了.在前两篇java实现网络爬虫和heritrix实现网络爬虫的基础上,这一次是要完整的做一次数据的收集.数据上传.数据分析.数据结果读取.数据可视化. 需要用到 ...
- Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法
最近在学习传智播客吴超老师的Hadoop视频,里面他在讲解RPC通信原理的过程中给了一个RPC的小例子,但是自己编写的过程中遇到一个小错误,整理如下: log4j:WARN No appenders ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- Hadoop中MR程序的几种提交运行模式
本地模型运行 1:在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路径下(c:/wc ...
- hadoop中遇到的一些问题
1.验证词统计程序.----无法加载本地库 出现错误: WARN util.NativeCodeLoader: Unable to load native-hadoop library for you ...
- Hadoop中客户端和服务器端的方法调用过程
1.Java动态代理实例 Java 动态代理一个简单的demo:(用以对比Hadoop中的动态代理) Hello接口: public interface Hello { void sayHello(S ...
- Hadoop中WritableComparable 和 comparator
1.WritableComparable 查看HadoopAPI,如图所示: WritableComparable继承自Writable和java.lang.Comparable接口,是一个Writa ...
随机推荐
- [转]jQuery实现图片轮播效果,jQuery实现焦点新闻
本文转自:http://blog.csdn.net/tsyj810883979/article/details/8986157 效果图: 实现代码: <!DOCTYPE html> < ...
- WSGI学习系列Paste
Paste has been under development for a while, and has lots of code in it. The code is largely decoup ...
- windows 7下安装MySQL5.6
一. 软件下载 从MySql官网上下载响应的版本,我的是5.6.17. 二.安装过程 以管理员权限运行安装程序,收集信息. 选择安装MySql产品,如果之前有安装过,那么就选择更新了. 同意Licen ...
- Json的访问
JSON:JavaScript 对象表示法(JavaScript Object Notation) 写法:名称/值对 访问方法:可以通过 data.名称 访问,也可以通过 data['名称'] 访问 ...
- Hibernate系列4-----之删除
1.和它的增改查兄弟不同,多了个until包定义了HibernateUntil类,让我们来一起看看吧 public class HibernateUntil { private static Conf ...
- 从零开始的全栈工程师——js篇(正则表达式)
正则 就是一条规则 用来检验字符串的格式 目标就是字符串 只要是通过表单提交的数据 都是字符串1.正则定义var reg = new RegExp( )var reg = /格式/ <--简写 ...
- 对数组排序进行"洗牌"(随机排序)
这段代码在这里使用Fisher Yates洗牌算法给一个指定的数组进行洗牌(随机排序). function shuffle(arr) { var i, j, tem ...
- ajax提交 返回值为undefined
easyui form 表单提交成功,但是返回值为undefined,原因是返回值不是json格式,是字符串的格式,那么解决办法就是把其转化成json格式 示例: $(function () { / ...
- (十)JavaScript之【DOM定义】
DOM定义Document Object Model 文档对象模型 是干什么的?改变 HTML 元素的内容 (innerHTML)改变 HTML 元素的样式 (CSS)改变 HTML 元素的属性对 H ...
- .net 使用Memcached
1.创建个人MemcachedHelper类 /// <summary> /// 页 面 名:缓存管理类<br/> /// 说 明:设置.获取.移除Cache<br/&g ...