Skip to content

Commit

Permalink
Integer的缓存池
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwanpp committed Dec 8, 2017
1 parent 6527c1b commit 8ee182e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
1 change: 0 additions & 1 deletion JDK7源码学习.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" />
</component>
</module>
2 changes: 1 addition & 1 deletion jdkTest/jdkTest.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
Expand Down
23 changes: 23 additions & 0 deletions jdkTest/src/com/jdk/test/TestHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jdk.test;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
* @author ÍõƼ
* @date 2017/12/8 0008
*/
public class TestHashMap {

@Test
public void indexForBucket() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
HashMap<String, String> map = new HashMap<>();
Method hash = map.getClass().getDeclaredMethod("hash", Object.class);
hash.setAccessible(true);
int hashOfWanwanpp = (int)hash.invoke(map, "wanwanpp");
System.out.println(Integer.toBinaryString(hashOfWanwanpp));
}
}
15 changes: 13 additions & 2 deletions jdkTest/src/com/jdk/test/TestUnsafe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jdk.test;

import org.junit.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
Expand All @@ -24,7 +25,7 @@ public class TestUnsafe {
// }


@org.junit.Test
@Test
public void testUnsafePut() throws NoSuchFieldException, IllegalAccessException {
// 通过反射得到theUnsafe对应的Field对象
Field field = Unsafe.class.getDeclaredField("theUnsafe");
Expand All @@ -49,8 +50,18 @@ public void testUnsafePut() throws NoSuchFieldException, IllegalAccessException

System.out.println(user);//打印midified-name,100,101,100.1
}
}

@Test
//在指定address写入char。
//操作不成功,直接fatal error。
public void testPutChar() throws Exception {
char s = 'x';
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
unsafe.putChar(System.identityHashCode(s), '2');
}
}

class User {
private String name = "test";
Expand Down
1 change: 1 addition & 0 deletions src/java/lang/Integer.java
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ private static class IntegerCache {

cache = new Integer[(high - low) + 1];
int j = low;
//将cache[]数组缓存填充满。
for (int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
Expand Down
3 changes: 3 additions & 0 deletions src/sun/misc/Unsafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public static Unsafe getUnsafe() {
*
* @see #putInt(Object, int, int)
*/
// o位需要修改的对象
// offset为相对对象的偏移地址
// x为为该偏移地址上赋的值。
public native void putObject(Object o, long offset, Object x);

/**
Expand Down

0 comments on commit 8ee182e

Please sign in to comment.