代码
package stopworld;
import java.util.HashMap;
public class StopTheWorld {
public static class MyThread extends Thread{
HashMap map = new HashMap();
public void run(){
while(true){
if(map.size()*512/1024/1024>=400){
map.clear();
System.out.println("clean map");
}
byte[] b1;
for(int i=0;i<100;i++){
b1 = new byte[512];
map.put(System.nanoTime(), b1);
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static class PrintThread extends Thread{
public static final long start = System.currentTimeMillis();
public void run(){
while(true){
long t = System.currentTimeMillis()-start;
System.out.println(t*1.0/1000);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String args[]){
MyThread s1 = new MyThread();
PrintThread p1 = new PrintThread();
s1.start();
p1.start();
}
}
控制台打印
0.0
0.101
0.202
0.302
0.402
0.503
0.603
0.703
0.803
0.903
1.004
1.104
1.204
1.304
1.405
1.505
1.605
1.707
1.807
1.907
2.007
2.107
2.207
2.433
2.533
2.633
2.733
2.833
2.933
3.034
3.134
3.234
3.334
3.434
3.534
3.634
3.734
3.835
3.935
4.035
4.135
4.235
4.341
4.442
4.542
4.642
4.742
4.936
5.036
5.136
5.236
5.336
5.437
5.539
5.639
5.739
5.84
5.94
6.04
6.14
6.24
6.34
6.441
6.541
6.641
6.741
6.842
6.942
7.042
7.142
7.242
7.342
7.779
7.879
7.979
8.079
8.179
8.279
8.379
8.479
8.58
8.68
8.78
8.88
8.98
9.084
9.184
9.284
9.384
9.488
clean map
9.588
9.688
9.788
9.889
10.035
10.135
10.235
10.335
10.435
10.535
10.635
10.735
10.835
10.935
11.035
11.135
11.235
11.335
11.435
11.535
11.635
gc日志
2.357: [GC2.358: [DefNew: 139776K->17472K(157248K), 0.1518325 secs] 139776K->123039K(506816K), 0.1521195 secs] [Times: user=0.11 sys=0.05, real=0.15 secs]
4.832: [GC4.832: [DefNew: 157248K->17472K(157248K), 0.1802498 secs] 262815K->249974K(506816K), 0.1803705 secs] [Times: user=0.17 sys=0.00, real=0.18 secs]
7.482: [GC7.482: [DefNew: 157248K->157248K(157248K), 0.0000456 secs]7.483: [Tenured: 232502K->349567K(349568K), 0.3730566 secs] 389750K->383428K(506816K), [Perm : 2512K->2512K(21248K)], 0.3732327 secs] [Times: user=0.34 sys=0.00, real=0.37 secs]
9.985: [Full GC9.985: [Tenured: 349567K->31559K(349568K), 0.1265237 secs] 506815K->31559K(506816K), [Perm : 2512K->2512K(21248K)], 0.1266411 secs] [Times: user=0.13 sys=0.00, real=0.13 secs]