-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95a9bc3
commit ada5426
Showing
11 changed files
with
79,604 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## 3:类加载-初始化 | ||
|
||
1. 加载过程 | ||
1. Loading | ||
|
||
1. 双亲委派,主要出于安全来考虑 | ||
|
||
2. LazyLoading 五种情况 | ||
|
||
1. –new getstatic putstatic invokestatic指令,访问final变量除外 | ||
|
||
–java.lang.reflect对类进行反射调用时 | ||
|
||
–初始化子类的时候,父类首先初始化 | ||
|
||
–虚拟机启动时,被执行的主类必须初始化 | ||
|
||
–动态语言支持java.lang.invoke.MethodHandle解析的结果为REF_getstatic REF_putstatic REF_invokestatic的方法句柄时,该类必须初始化 | ||
|
||
3. ClassLoader的源码 | ||
|
||
1. findInCache -> parent.loadClass -> findClass() | ||
|
||
4. 自定义类加载器 | ||
|
||
1. extends ClassLoader | ||
2. overwrite findClass() -> defineClass(byte[] -> Class clazz) | ||
3. 加密 | ||
|
||
5. 混合执行 编译执行 解释执行 | ||
|
||
1. 检测热点代码:-XX:CompileThreshold = 10000 | ||
2. Linking | ||
1. Verification | ||
2. Preparation | ||
3. Resolution | ||
3. Initializing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.mashibing.jvm; | ||
|
||
public class Hello { | ||
public void m() { | ||
System.out.println("Hello JVM!"); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/com/mashibing/jvm/classloader/T007_MSBClassLoaderWithEncription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.mashibing.jvm.classloader; | ||
|
||
import com.mashibing.jvm.Hello; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
|
||
public class T007_MSBClassLoaderWithEncription extends ClassLoader { | ||
|
||
public static int seed = 0B10110110; | ||
|
||
@Override | ||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
File f = new File("c:/test/", name.replace('.', '/').concat(".msbclass")); | ||
|
||
try { | ||
FileInputStream fis = new FileInputStream(f); | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
int b = 0; | ||
|
||
while ((b=fis.read()) !=0) { | ||
baos.write(b ^ seed); | ||
} | ||
|
||
byte[] bytes = baos.toByteArray(); | ||
baos.close(); | ||
fis.close();//可以写的更加严谨 | ||
|
||
return defineClass(name, bytes, 0, bytes.length); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return super.findClass(name); //throws ClassNotFoundException | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
encFile("com.mashibing.jvm.hello"); | ||
|
||
ClassLoader l = new T007_MSBClassLoaderWithEncription(); | ||
Class clazz = l.loadClass("com.mashibing.jvm.Hello"); | ||
Hello h = (Hello)clazz.newInstance(); | ||
h.m(); | ||
|
||
System.out.println(l.getClass().getClassLoader()); | ||
System.out.println(l.getParent()); | ||
} | ||
|
||
private static void encFile(String name) throws Exception { | ||
File f = new File("c:/test/", name.replace('.', '/').concat(".class")); | ||
FileInputStream fis = new FileInputStream(f); | ||
FileOutputStream fos = new FileOutputStream(new File("c:/test/", name.replaceAll(".", "/").concat(".msbclass"))); | ||
int b = 0; | ||
|
||
while((b = fis.read()) != -1) { | ||
fos.write(b ^ seed); | ||
} | ||
|
||
fis.close(); | ||
fos.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.mashibing.jvm.classloader; | ||
|
||
public class T008_LazyLoading { //严格讲应该叫lazy initialzing,因为java虚拟机规范并没有严格规定什么时候必须loading,但严格规定了什么时候initialzing | ||
public static void main(String[] args) throws Exception { | ||
//P p; | ||
//X x = new X(); | ||
//System.out.println(P.i); | ||
//System.out.println(P.j); | ||
Class.forName("com.mashibing.jvm.classloader.T008_LazyLoading$P"); | ||
|
||
} | ||
|
||
public static class P { | ||
final static int i = 8; | ||
static int j = 9; | ||
static { | ||
System.out.println("P"); | ||
} | ||
} | ||
|
||
public static class X extends P { | ||
static { | ||
System.out.println("X"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mashibing.jvm.classloader; | ||
|
||
public class T009_WayToRun { | ||
public static void main(String[] args) { | ||
for(int i=0; i<10_0000; i++) | ||
m(); | ||
|
||
long start = System.currentTimeMillis(); | ||
for(int i=0; i<10_0000; i++) { | ||
m(); | ||
} | ||
long end = System.currentTimeMillis(); | ||
System.out.println(end - start); | ||
} | ||
|
||
public static void m() { | ||
for(long i=0; i<10_0000L; i++) { | ||
long j = i%3; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mashibing.jvm.cms; | ||
|
||
public class Student { | ||
private String name; | ||
private int age; | ||
|
||
public Student(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.mashibing.jvm.cms; | ||
|
||
|
||
import java.util.ArrayList; | ||
|
||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
|
||
|
||
ArrayList<Student> list = new ArrayList<>(); | ||
for(int i = 0;i<6000000;i++){ | ||
list.add(new Student("msb"+i,i)); | ||
} | ||
|
||
try { | ||
Thread.sleep(10000000000L); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.mashibing.jvm.gc; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* 从数据库中读取信用数据,套用模型,并把结果进行记录和传输 | ||
*/ | ||
|
||
public class T15_FullGC_Problem01 { | ||
|
||
private static class CardInfo { | ||
BigDecimal price = new BigDecimal(0.0); | ||
String name = "张三"; | ||
int age = 5; | ||
Date birthdate = new Date(); | ||
|
||
public void m() { | ||
|
||
} | ||
} | ||
|
||
private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50, | ||
new ThreadPoolExecutor.DiscardOldestPolicy()); | ||
|
||
public static void main(String[] args) throws Exception { | ||
executor.setMaximumPoolSize(50); | ||
|
||
for (;;){ | ||
modelFit(); | ||
Thread.sleep(100); | ||
} | ||
} | ||
|
||
private static void modelFit(){ | ||
List<CardInfo> taskList = getAllCardInfo(); | ||
taskList.forEach(info -> { | ||
// do something | ||
executor.scheduleWithFixedDelay(() -> { | ||
//do sth with info | ||
info.m(); | ||
|
||
}, 2, 3, TimeUnit.SECONDS); | ||
}); | ||
} | ||
|
||
private static List<CardInfo> getAllCardInfo(){ | ||
List<CardInfo> taskList = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 100; i++) { | ||
CardInfo ci = new CardInfo(); | ||
taskList.add(ci); | ||
} | ||
|
||
return taskList; | ||
} | ||
} |
Oops, something went wrong.