Java learning summary: 33 (System class)

System class

System class methods

No. method Types of description
1 public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) ordinary Array paste operation
2 public static long currentTimeMillis() ordinary Get the current date and time and return as long data
3 public static void gc() ordinary Garbage collection

currentTimeMillis () method

Example: Observe the use of currentTimeMillis () method

package Project.Study.SystemClass;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test1 {
    public static void main(String[]args){
    	//取得开始时间
        long start=System.currentTimeMillis();	//currentTimeMillis()返回以毫秒为单位的当前时间
        String str="";
        for(int x=0;x<30000;x++){
            str+=x;
        }
        long end=System.currentTimeMillis();    //currentTimeMillis()返回以毫秒为单位的当前时间
        System.out.println("开始时间:"+start);
        SimpleDateFormat start1 = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
        Date date1 = new Date(start);
        System.out.println(start1.format(date1));
        System.out.println("结束时间:"+end);
        SimpleDateFormat end2 = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
        Date date2 = new Date(end);
        System.out.println(end2.format(date2));
        System.out.println("本次操作所花费的时间:"+(end-start));
    }
}
//结果:
//开始时间:1585573645695
//2020年-03月30日-21时07分25秒
//结束时间:1585573646162
//2020年-03月30日-21时07分26秒
//本次操作所花费的时间:467(毫秒)

System class gc () method

The gc () method is not a new operation method, it is an indirect call to the gc () method in the Runtime class, and does not mean a rewritten method. So the final effect of calling System.gc () and calling Runtime.getRuntime (). Gc () is exactly the same.

Object recycling

If you want to generate an object, you can handle some of the operations when the object is generated through the construction method. Similarly, when an object is recycled, we can also do some finishing work, which can be achieved by the finalize () method, which is defined by the Object class. Object recycling methods are as follows:

protected void finalize() throws Throwable

Example: Object recycling operation

package Project.Study.SystemClass;

class Human{
    public Human(){
        System.out.println("一个健康的孩子出生了");
    }
    protected void finalize()throws Throwable{		//覆写Object类方法
        System.out.println("修仙活了200年,到时候了");
        throw new Exception("此处即使抛出异常对象也不会产生任何影响");
    }
}
public class Test2 {
    public static void main(String[]args){
        Human men=new Human();	//实例化新的对象
        men=null;				//产生垃圾
        System.gc();			//手工处理垃圾收集
    }
}
//结果:
//一个健康的孩子出生了
//修仙活了200年,到时候了

Through the above program, we can see that when the heap memory space of an object is about to be recycled, the finalize () method will be automatically called, so that some finishing work before object recycling can be performed. And even if this method produces any exceptions or errors, it will not affect the normal execution of the program.

49 original articles published · Liked25 · Visits1514

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/105208233
Recommended