Java使用Unsafe接口操作数组Demo
2024-08-29 21:45:00
public class unSafeArrayDemo { private static final sun.misc.Unsafe UNSAFE;
private static final long TBASE;
private static final int TSHIFT; static {
int ts;
try {
UNSAFE = getUnsafe();
TBASE = UNSAFE.arrayBaseOffset(String[].class);
ts = UNSAFE.arrayIndexScale(String[].class);
} catch (Exception e) {
throw new Error(e);
}
TSHIFT = 31 - Integer.numberOfLeadingZeros(ts);
} static Unsafe getUnsafe() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe)f.get(null);
return unsafe;
} @SuppressWarnings("unchecked")
static final String entryAt(String[] tab, int i) {
return (String) UNSAFE.getObjectVolatile(tab, ((long)i << TSHIFT) + TBASE);
} public static void main(String[] args) {
int nLen = 37;
String[] table = new String[nLen];
for (int i = 0; i < nLen; i++) {
table[i] = "数值为: " + String.valueOf(i + 26);
}
String str = entryAt(table, 23);
System.out.println(str);
} }
最新文章
- #研发解决方案#分布式并行计算调度和管理系统Summoner
- C++调用shell
- 网站实现特定某个地区访问执行跳转(js方法)
- Android性能优化(一)
- JS insertAdajcentHTML 方法简介
- centos6.4yum搭建lamp环境
- 中文编码、字符集,GBK, UTF-8的概念
- TP中不区分大小写__APP__和__URL__的注意事项
- C++ 读写文件流
- paper 7:支持向量机系列四:Outliers —— 介绍支持向量机使用松弛变量处理 outliers 方法。
- socket 发送发送HTTP请求
- Codeforces 15B Laser
- HDU 1853Cyclic Tour(网络流之最小费用流)
- 织梦dedecms返回上一级链接代码
- ThinkPHP使用方法
- c/c++浮点数在内存中存储方式
- 模块的语法 import ,from...import....
- 1053. Path of Equal Weight (30)
- CrackME 2011 # 2 逆向练习解题思路
- ASP.NET MVC 常用路由总结