?? ex_5_3_1.java
字號:
/*
*文件名:ex_5_3_1.java
*說 明:垃圾回收機制舉例
*/
// Chair類
class Chair {
static boolean gcrun = false;
static boolean f = false;
static int created = 0;
static int finalized = 0;
int i;
Chair() {
i = ++created;
if(created == 47)
System.out.println("Created 47");
}
public void finalize() {
if(!gcrun) {
//第一次調用finalize()方法
gcrun = true;
System.out.println(
"在" +
created + "個Chair對象創建之后開始調用finalize()方法");
}
if(i == 47) {
System.out.println(
"finalize() #47, " +
"正在設置創建終止信號");
f = true;
}
finalized++;
if(finalized >= created)
System.out.println(
"所有" + finalized + " 個Chair對象都被清除掉");
}
}
// 主類
public class ex_5_3_1 {
public static void main(String[] args) {
// 如果f沒有被設置,則創建Chairs對象和字符串對象
while(!Chair.f) {
new Chair();
new String("To take up space");
}
System.out.println(
"Chair對象被創建結束:\n" +
"總數為 " + Chair.created +
", 調用finalize()次數為 " + Chair.finalized);
// GC選項
if(args.length > 0) {
if(args[0].equals("gc") ||
args[0].equals("all")) {
System.out.println("gc():");
System.gc();
}
if(args[0].equals("finalize") ||
args[0].equals("all")) {
System.out.println("runFinalization():");
System.runFinalization();
}
}
System.out.println("bye!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -