On the System class

System class

Method of operation

Get current system time

currentTemiMillis()

public static long currenTimeMillis() ;
  • Example:

Certain operations execution time statistics

public class TestDemo {
    public static void main(String [] args) {
        long start = System.currentTimeMillis(); // 开始时间
        String str = "" ;
        for (int x = 0; x < 30000 ; x++) {
            str += x ; 
        }
        long end = System.currentTimeMillis(); //结束时间
        System.out.println("Time = " + (end - start));// 单位ms
    }
}
// 执行结果:(单位:ms)
Time = 2246

system.gc()

  • GC System class method, not a new method of GC, GC method instead calls the Runtime class
public static void gc() ;
  • Leads to:

    Constructor will call the class object generator performs some processing operations, but the object is generated if a GC recovered, but Java is provided a method of a block of code can be executed before the object is recovered GC --finzlize () method

finalize () method

protected void finalize() throws Throwable 

Throwable: no mess any errors, are executing the program

class Member {
    public Member() {
        System.out.println("open");
    }
    @Override
    protected void finalize() throws Throwable {
        System.out.println("end");
        throw new Exception("……");  // 抛出异常
    }
}

public class TestDemo {
    public static void main(String [] args) {
        Member men = new Member() ;
        men = null ; // 对象成为了垃圾
        System.gc(); // 手工GC垃圾处理
    }
}

Program execution: men = null become garbage objects, and then recovered GC manually trigger Finalize (), execute a predetermined program block method. (Equivalent to calling finzlize before the GC garbage collection ())

——

Construction method for use when an object is initialized, the finalize () method is used for the object prior to being recovered GC.

  • The difference between final, finally, finalize the three
  • final: Java keyword, class definition can not be inherited, the method can not be overridden and constant
  • finally: Java keywords, unusual unity export
  • finalize: built-in method, public static void finzlize () throws Throwable; execution method before the program object GC recovery, even if it will not lead to abnormal program interruption

Guess you like

Origin www.cnblogs.com/wangyuyang1016/p/11094468.html