Java中的finalize方法的问题

时间:2018-11-07 02:31:00

标签: java finalize

在上一练习中,我对finalize方法遇到问题。好像我有一个过时的方法,而且我似乎找不到finalize的最新版本。我有两个文件,需要调用“ finalize”方法。有人可以给我一个确定方法的例子还是可以帮助我?这是我的代码:

public class RoomOccupancy {

    private int numberInRoom;
    private static int totalNumber;
    private double roomCharges;
    private boolean finalizeCalled;

    public RoomOccupancy () {
        this.numberInRoom = 0;
        this.totalNumber = 0;
        this.roomCharges = 0;
        this.finalizeCalled = false;
        System.out.println ( "Room Occupancy - default/no argument constructor" );
    }

    public RoomOccupancy (int number, double rCharges) {
        numberInRoom = number;
        roomCharges = rCharges;
        finalizeCalled = false;
        System.out.println ( "Room Occupancy - Overloaded/2-argument constructor" );
    }

    public static int getTotal () {
        return totalNumber;
    }

    public void addOneToRoom () {
        numberInRoom++;
        totalNumber++;
    }

    public void removeOneFromRoom () {
        numberInRoom--;
        totalNumber--;
    }

    public int getNumber () {
        return numberInRoom;
    }

    public void setNumber (int number) {
        this.numberInRoom = number;
    }

    public double getCharges () {
        return roomCharges;
    }

    public void setCharges (double rCharges) {
        this.roomCharges = rCharges;
    }

    public String toString () {
        String c;
        c = " " + numberInRoom + " " + roomCharges + " ";
        return c;
    }

    public boolean equals ( Object obj ) {
        if ( this == obj ) return true;

        if (( obj != null) && ( getClass() == obj.getClass())) {
            RoomOccupancy d = ( RoomOccupancy ) obj;

            if (( numberInRoom == d.numberInRoom) &&
                ( totalNumber == d.totalNumber) &&
                ( roomCharges == d.roomCharges)) {
                return true;
            }
            else {
                return false;
            }

        }
        else {
            return false;
        }

    }

    public void finalize ( ) {
        if ( !finalizeCalled ) {
            // Do cleanup
        }
        System.out.println ( "Course - finalize method" );

    }

    public void dispose ( ) {
        //Do cleanup
        finalizeCalled = true;
        System.out.println ( "Course - finalize method" );
    }
}

import java.text.DecimalFormat;

public class RoomOccupancyTest {

    public static void main ( String [] args ) {

        RoomOccupancy noOccupancy = new RoomOccupancy ( );
        System.out.println ("No Occupancy =" + noOccupancy);
        RoomOccupancy roomA = new RoomOccupancy(0, 100.00);
        RoomOccupancy roomB = new RoomOccupancy(0, 200.00);
        RoomOccupancy roomC = new RoomOccupancy(0, 250.00);
        DecimalFormat patternCharges = new DecimalFormat("#####0.00");

        System.out.println ("Five people have checked into room A at $100.00 per person\n" +
                            "Four people have checked into room B at $200.00 per person.\n" +
                            "Three people have checked into room C at $250.00 per person.");
        roomA.addOneToRoom();
        roomA.addOneToRoom();
        roomA.addOneToRoom();
        roomA.addOneToRoom();
        roomA.addOneToRoom();
        roomB.addOneToRoom();
        roomB.addOneToRoom();
        roomB.addOneToRoom();
        roomB.addOneToRoom();
        roomC.addOneToRoom();
        roomC.addOneToRoom();
        roomC.addOneToRoom();

        System.out.println ("Room A holds " + roomA.getNumber() + " The total charge is: $" + (patternCharges.format(roomA.getCharges() * roomA.getNumber())));
        System.out.println ("Room B holds " + roomB.getNumber() + " The total charge is: $" + (patternCharges.format(roomB.getCharges() * roomB.getNumber())));
        System.out.println ("Room C holds " + roomC.getNumber() + " The total charge is: $" + (patternCharges.format(roomC.getCharges() * roomC.getNumber())));
        System.out.println ("Total in all rooms is " + RoomOccupancy.getTotal());

        System.out.println ("One person from each room has left.");
        roomA.removeOneFromRoom();
        roomB.removeOneFromRoom();
        roomC.removeOneFromRoom();

        System.out.println ("Room A holds " + roomA.getNumber() + " The total charge is: $" + (patternCharges.format(roomA.getCharges() * roomA.getNumber())));
        System.out.println ("Room B holds " + roomB.getNumber() + " The total charge is: $" + (patternCharges.format(roomB.getCharges() * roomB.getNumber())));
        System.out.println ("Room C holds " + roomC.getNumber() + " The total charge is: $" + (patternCharges.format(roomC.getCharges() * roomC.getNumber())));
        System.out.println ("Total in all rooms is " + RoomOccupancy.getTotal());

        if (roomA == roomB)
            System.out.println ("Room A and B are the same object."); // NOT EXPECTED
        if (roomA == roomC)
            System.out.println ("Room A and C are the same object."); // NOT EXPECTED
        if (roomB == roomC)
            System.out.println ("Room B and C are the same object."); // NOT EXPECTED
        else
            System.out.println ("No rooms are the same object."); // EXPECTED

        if (roomA.equals (roomB))
            System.out.println ( "Room A and B are EQUAL."); // NOT EXPECTED
        if (roomA.equals(roomC))
            System.out.println ( "Room A and C are the EQUAL."); // NOT EXPECTED
        if (roomB.equals(roomC))
            System.out.println ( "Room B and C are the EQUAL."); // NOT EXPECTED
        else
            System.out.println ( "No Rooms are EQUAL."); // EXPECTED

        roomA.finalize();
        roomB.finalize();
        roomC.finalize();

        roomA = roomB = roomC = null;
        System.gc();


    }
}

1 个答案:

答案 0 :(得分:2)

从Java 9开始不推荐使用

finalize。您可以在javadoc中看到替代的原因和替代方法:-

  

已弃用。最终确定机制本质上存在问题。   终结处理可能会导致性能问题,死锁和挂起。   终结器中的错误可能导致资源泄漏;没有办法   如果不再需要取消定稿;而且没有订购   在调用中指定以终结不同对象的方法。   此外,我们无法保证   定案。 finalize方法可以在finalizable上调用   仅在无限期延迟后才反对。其班级   实例拥有非堆资源应提供一种启用方法   明确释放这些资源,它们也应实施   适当时自动关闭。 Cleaner和PhantomReference提供   一种更灵活有效的方法来释放对象时的资源   变得无法访问。

您不应使用此方法。实际上,您不需要!

如果仍然要使用它,请忽略警告或在您的方法上添加以下内容以隐藏警告:-

@SuppressWarnings( "deprecation" )