复制代码
- //:Garbage.java
- //Demonstrationofthegarbage
- //collectorandfinalization
- classChair{
- staticbooleangcrun=false;
- staticbooleanf=false;
- staticintcreated=0;
- staticintfinalized=0;
- inti;
- Chair(){
- i=++created;
- if(created==47)
- System.out.println("Created47");
- }
- protectedvoidfinalize(){
- if(!gcrun){
- gcrun=true;
- System.out.println(
- "Beginningtofinalizeafter"+
- created+"Chairshavebeencreated");
- }
- if(i==47){
- System.out.println(
- "FinalizingChair#47,"+
- "SettingflagtostopChaircreation");
- f=true;
- }
- finalized++;
- if(finalized>=created)
- System.out.println(
- "All"+finalized+"finalized");
- }
- }
- publicclassGarbage{
- publicstaticvoidmain(String[]args){
- if(args.length==0){
- System.err.println("Usage:
- "+
- "javaGarbagebefore
- or:
- "+
- "javaGarbageafter");
- return;
- }
- while(!Chair.f){
- newChair();
- newString("Totakeupspace");
- }
- System.out.println(
- "AfterallChairshavebeencreated:
- "+
- "totalcreated="+Chair.created+
- ",totalfinalized="+Chair.finalized);
- if(args[0].equals("before")){
- System.out.println("gc():");
- System.gc();
- System.out.println("runFinalization():");
- System.runFinalization();
- }
- System.out.println("bye!");
- if(args[0].equals("after"))
- System.runFinalizersOnExit(true);
- }
- }///:~
复制代码
- Created47
- Beginningtofinalizeafter8694Chairshavebeencreated
- FinalizingChair#47,SettingflagtostopChaircreation
- AfterallChairshavebeencreated:
- totalcreated=9834,totalfinalized=108
- bye!
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |