Java - getting existing object time

Say I created an object like this

Object obj = new Object();

Now I want to find out how long the object has existed, for example

System.out.println("Creating object");
Object obj = new Object();
//Print 0
System.out.println("The object has existed for (ms): " + getTime(obj));
Thread.sleep(1000);
//Print 1000
System.out.println("The object has existed for (ms): " + getTime(obj));

How can i do this? Any help is appreciated.

+4
source share
3 answers

Try this code. finalize () is fired when the object is destroyed. If the object goes to the garbage collector, which terminates the method. in my code System.gc () use to call garbase collector. This code is very long. its just for understanding the purpose. You can customize this code.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Thamira
 */
public class Demostra {

    public static void main(String[] args) {

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

        System.out.println("Creating object :" + sdf.format(cal.getTime()));
        Object obj = new Object() {

            @Override
            protected void finalize() throws Throwable {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                System.out.println("The object has removed for : " + sdf.format(cal.getTime()));

                super.finalize(); //To change body of generated methods, choose Tools | Templates.
            }

        };

        cal = Calendar.getInstance();
        sdf = new SimpleDateFormat("HH:mm:ss");

        System.out.println("The object has existed for : " + sdf.format(cal.getTime()));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Demostra.class.getName()).log(Level.SEVERE, null, ex);
        }

        cal = Calendar.getInstance();
        sdf = new SimpleDateFormat("HH:mm:ss");

        System.out.println("The object has existed for  : " + sdf.format(cal.getTime()));

        obj = null;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Demostra.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
+3
source

, getTime(obj). - , , System.currentTimeMillis(), , , .

, , , , , , ​​ .

+3

TL;DR

Duration.between( objectBorn , Instant.now() )

java.time

lakmal , , , , java.time.

, Java . .

Instant

Instant UTC ( (9) ).

Instant instant = Instant.now();  // Current moment in UTC.

Java 8, ( , ). Java 9 Clock , . , Java 8, Java 9 ; Java 8.

Duration

Duration .

.

package javatimestuff;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Basil Bourque
 */
public class Life {

    private Instant objectBorn = null;

    // Constructor
    public Life () {
        this.objectBorn = Instant.now ();  // Capture the moment at birth of this object.
    }

    public Duration objectAgeAsDuration () {
        Duration d = Duration.between ( objectBorn , Instant.now () );
        return d;
    }

    public long objectAgeAsNanos () {
        Duration d = this.objectAgeAsDuration ();
        return d.toNanos ();
    }

}

.

    public static void main ( String[] args ) {
        System.out.println ( "Please wait a few seconds as we birth a new `Life` object. " );

        Life l = new Life ();
        try {
            Thread.sleep ( TimeUnit.SECONDS.toMillis ( 3 ) );
        } catch ( InterruptedException ex ) {
            // Interruption may be this thread being woken.
            // …
        }

        System.out.println ( "The Life object’s age as Duration: " + l.objectAgeAsDuration () );
        System.out.println ( "The Life object’s age as nanoseconds: " + l.objectAgeAsNanos () );
    }

, Life.

: PT3.002S

Life Life : 3010000000

finalize

finalize. :

  • finalize , , . , . - Java, Java.
  • finalize . , JVM , , finalize .

, finalize . (teardown, shutdown, , ), , .

Object::finalize() Java 9 . :

. . , . ; , ; . , . finalize , , , . , , , , AutoCloseable. Cleaner PhantomReference , .


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+2

Source: https://habr.com/ru/post/1667409/


All Articles