⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 garbagecollectcanvas.java

📁 深入java 虚拟机中的一个Java程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* Copyright (c) 1996, 1997 Bill Venners. All Rights Reserved.
*
* This Java source file is part of the Interactive Illustrations Web
* Site, which is delivered in the applets directory of the CD-ROM
* that accompanies the book "Inside the Java Virtual Machine" by Bill
* Venners, published by McGraw-Hill, 1997,ISBN: 0-07-913248-0. This
* source file is provided for evaluation purposes only, but you can
* redistribute it under certain conditions, described in the full
* copyright notice below.
*
* Full Copyright Notice:
*
* All the web pages and Java applets delivered in the applets
* directory of the CD-ROM, consisting of ".html," ".gif," ".class,"
* and ".java" files, are copyrighted (c) 1996, 1997 by Bill
* Venners, and all rights are reserved.  This material may be copied
* and placed on any commercial or non-commercial web server on any
* network (including the internet) provided that the following
* guidelines are followed:
*
* a. All the web pages and Java Applets (".html," ".gif," ".class,"
* and ".java" files), including the source code, that are delivered
* in the applets directory of the CD-ROM that
* accompanies the book must be published together on the same web
* site.
*
* b. All the web pages and Java Applets (".html," ".gif," ".class,"
* and ".java" files) must be published "as is" and may not be altered
* in any way.
*
* c. All use and access to this web site must be free, and no fees
* can be charged to view these materials, unless express written
* permission is obtained from Bill Venners.
*
* d. The web pages and Java Applets may not be distributed on any
* media, other than a web server on a network, and may not accompany
* any book or publication.
*
* BILL VENNERS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  BILL VENNERS
* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY A LICENSEE AS A
* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES.
*/
import java.awt.*;

/**
* This class is the canvas upon which is drawn the graphical
* depiction of the garbage collection process shown in
* the garbage collect mode. All this drawing is done by
* the paint() method of this class. This class also implements
* the functionality of the garbage collection process.
*
* @author  Bill Venners
*/
public class GarbageCollectCanvas extends Canvas {

    private GCHeap gcHeap;
    private LocalVariables localVars;
    private HeapOfFishTextArea controlPanelTextArea;

    private Color currentGCMarkNodeColor = Color.magenta;

    private final int poolImageInsets = 5;
    private final int localVarStringMargin = 5;

    private int localVarRectWidth;
    private int localVarRectHeight;
    private int xLocalVarRectStart;
    private int yYellowFishLocalVarStart;
    private int yBlueFishLocalVarStart;
    private int yRedFishLocalVarStart;

    // Fish area is just to the left of the local variables.
    private int xFishAreaStart;

    // State variables for the garbage collector
    private final int garbageCollectorHasNotStarted = 0;

    private final int startingAtYellowLocalVariableRoot = 1;
    private final int traversingFromYellowLocalVariableRoot = 2;
    private final int doneWithYellowLocalVariableRoot = 3;

    private final int startingAtBlueLocalVariableRoot = 4;
    private final int traversingFromBlueLocalVariableRoot = 5;
    private final int doneWithBlueLocalVariableRoot = 6;

    private final int startingAtRedLocalVariableRoot = 7;
    private final int traversingFromRedLocalVariableRoot = 8;
    private final int doneWithRedLocalVariableRoot = 9;

    private final int readyToSweepUnmarkedFish = 10;
    private final int doneSweepingUnmarkedFish = 11;

    private final int garbageCollectorIsDone = 12;

    private int currentGCState = garbageCollectorHasNotStarted;

    private boolean fishAreBeingMarked;
    private int currentFishBeingMarked;

    private boolean yellowFishLocalVarIsCurrentGCMarkNode;
    private boolean blueFishLocalVarIsCurrentGCMarkNode;
    private boolean redFishLocalVarIsCurrentGCMarkNode;

    private Color yellowFishLocalVarLineColor;
    private Color blueFishLocalVarLineColor;
    private Color redFishLocalVarLineColor;

    GarbageCollectCanvas(GCHeap heap, LocalVariables locVars, HeapOfFishTextArea ta) {
        setBackground(Color.blue);
        gcHeap = heap;
        localVars = locVars;
        controlPanelTextArea = ta;
    }

    public void nextGCStep() {

        switch (currentGCState) {

        case garbageCollectorHasNotStarted:
            yellowFishLocalVarIsCurrentGCMarkNode = true;
            currentGCState = startingAtYellowLocalVariableRoot;
            controlPanelTextArea.setText(HeapOfFishStrings.traversingYellowRoot);
            break;

        case startingAtYellowLocalVariableRoot:

            yellowFishLocalVarIsCurrentGCMarkNode = false;
            if (localVars.yellowFish != 0) {

                ObjectHandle oh = gcHeap.getObjectHandle(localVars.yellowFish);
                yellowFishLocalVarIsCurrentGCMarkNode = false;
                oh.myColor = Color.gray;
                yellowFishLocalVarLineColor = Color.gray;
                currentFishBeingMarked = localVars.yellowFish;
                fishAreBeingMarked = true;
                currentGCState = traversingFromYellowLocalVariableRoot;
            }
            else {
                blueFishLocalVarIsCurrentGCMarkNode = true;
                currentGCState = startingAtBlueLocalVariableRoot;
                controlPanelTextArea.setText(HeapOfFishStrings.traversingBlueRoot);
            }
            break;

        case traversingFromYellowLocalVariableRoot:

            boolean doneWithThisTree = traverseNextFishNode();
            if (doneWithThisTree) {
                ObjectHandle oh = gcHeap.getObjectHandle(localVars.yellowFish);
                yellowFishLocalVarLineColor = Color.black;
                oh.myColor = Color.black;
                fishAreBeingMarked = false;
                yellowFishLocalVarIsCurrentGCMarkNode = true;
                currentGCState = doneWithYellowLocalVariableRoot;
                controlPanelTextArea.setText(HeapOfFishStrings.doneWithYellowRoot);
            }
            break;

        case doneWithYellowLocalVariableRoot:

            yellowFishLocalVarIsCurrentGCMarkNode = false;
            blueFishLocalVarIsCurrentGCMarkNode = true;
            currentGCState = startingAtBlueLocalVariableRoot;
            controlPanelTextArea.setText(HeapOfFishStrings.traversingBlueRoot);
            break;

        case startingAtBlueLocalVariableRoot:

            blueFishLocalVarIsCurrentGCMarkNode = false;
            if (localVars.blueFish != 0) {

                ObjectHandle oh = gcHeap.getObjectHandle(localVars.blueFish);
                blueFishLocalVarIsCurrentGCMarkNode = false;
                oh.myColor = Color.gray;
                blueFishLocalVarLineColor = Color.gray;
                currentFishBeingMarked = localVars.blueFish;
                fishAreBeingMarked = true;
                currentGCState = traversingFromBlueLocalVariableRoot;
            }
            else {
                redFishLocalVarIsCurrentGCMarkNode = true;
                currentGCState = startingAtRedLocalVariableRoot;
                controlPanelTextArea.setText(HeapOfFishStrings.traversingRedRoot);
            }
            break;

        case traversingFromBlueLocalVariableRoot:

            doneWithThisTree = traverseNextFishNode();
            if (doneWithThisTree) {
                ObjectHandle oh = gcHeap.getObjectHandle(localVars.blueFish);
                blueFishLocalVarLineColor = Color.black;
                oh.myColor = Color.black;
                fishAreBeingMarked = false;
                blueFishLocalVarIsCurrentGCMarkNode = true;
                currentGCState = doneWithBlueLocalVariableRoot;
                controlPanelTextArea.setText(HeapOfFishStrings.doneWithBlueRoot);
            }
            break;

        case doneWithBlueLocalVariableRoot:

            blueFishLocalVarIsCurrentGCMarkNode = false;
            redFishLocalVarIsCurrentGCMarkNode = true;
            currentGCState = startingAtRedLocalVariableRoot;
            controlPanelTextArea.setText(HeapOfFishStrings.traversingRedRoot);
            break;

        case startingAtRedLocalVariableRoot:

            redFishLocalVarIsCurrentGCMarkNode = false;
            if (localVars.redFish != 0) {

                ObjectHandle oh = gcHeap.getObjectHandle(localVars.redFish);
                redFishLocalVarIsCurrentGCMarkNode = false;
                oh.myColor = Color.gray;
                redFishLocalVarLineColor = Color.gray;
                currentFishBeingMarked = localVars.redFish;
                fishAreBeingMarked = true;
                currentGCState = traversingFromRedLocalVariableRoot;
            }
            else {
                currentGCState = readyToSweepUnmarkedFish;
                controlPanelTextArea.setText(HeapOfFishStrings.readyToSweepUnmarkedFish);
            }
            break;

        case traversingFromRedLocalVariableRoot:

            doneWithThisTree = traverseNextFishNode();
            if (doneWithThisTree) {
                ObjectHandle oh = gcHeap.getObjectHandle(localVars.redFish);
                redFishLocalVarLineColor = Color.black;
                oh.myColor = Color.black;
                fishAreBeingMarked = false;
                redFishLocalVarIsCurrentGCMarkNode = true;
                currentGCState = doneWithRedLocalVariableRoot;
                controlPanelTextArea.setText(HeapOfFishStrings.doneWithRedRoot);
            }
            break;

        case doneWithRedLocalVariableRoot:

            redFishLocalVarIsCurrentGCMarkNode = false;
            currentGCState = readyToSweepUnmarkedFish;
            controlPanelTextArea.setText(HeapOfFishStrings.readyToSweepUnmarkedFish);
            break;

        case readyToSweepUnmarkedFish:

            int objectsFreedCount = 0;
            for (int i = 0; i < gcHeap.getHandlePoolSize(); ++i) {
                ObjectHandle oh = gcHeap.getObjectHandle(i + 1);
                if (!oh.free && oh.myColor == Color.white) {
                    gcHeap.freeObject(i + 1);
                    ++objectsFreedCount;
                }
            }
            currentGCState = doneSweepingUnmarkedFish;
            String doneSweepingText = HeapOfFishStrings.sweptFish0 + objectsFreedCount
                + HeapOfFishStrings.sweptFish1;
            controlPanelTextArea.setText(doneSweepingText);
            break;

        case doneSweepingUnmarkedFish:
            currentGCState = garbageCollectorIsDone;
            controlPanelTextArea.setText(HeapOfFishStrings.garbageCollectionDone);
            break;

        case garbageCollectorIsDone:
        default:
            break;
        }
    }

    // Returns true if done with this tree.
    private boolean traverseNextFishNode() {
        ObjectHandle oh = gcHeap.getObjectHandle(currentFishBeingMarked);

        int myFriendIndex = gcHeap.getObjectPool(oh.objectPos);

        if ((myFriendIndex != 0) && (oh.myFriendLineColor == Color.white)) {
            oh.myFriendLineColor = Color.gray;
            ObjectHandle myFriend = gcHeap.getObjectHandle(myFriendIndex);
            myFriend.previousNodeInGCTraversalIsAFish = true;
            myFriend.previousFishInGCTraversal = currentFishBeingMarked;
            if (myFriend.myColor == Color.white) {
                myFriend.myColor = Color.gray;
            }
            currentFishBeingMarked = myFriendIndex;
            return false;
        }
        else if (oh.fish.getFishColor() == Color.yellow) {
            if (oh.previousNodeInGCTraversalIsAFish) {
                traverseBackFromGrayLine(oh.previousFishInGCTraversal);
                return false;
            }
            return true;
        }

        int myLunchIndex = gcHeap.getObjectPool(oh.objectPos + 1);

        if ((myLunchIndex != 0) && (oh.myLunchLineColor == Color.white)) {
            oh.myLunchLineColor = Color.gray;
            ObjectHandle myLunch = gcHeap.getObjectHandle(myLunchIndex);
            myLunch.previousNodeInGCTraversalIsAFish = true;
            myLunch.previousFishInGCTraversal = currentFishBeingMarked;
            if (myLunch.myColor == Color.white) {
                myLunch.myColor = Color.gray;
            }
            currentFishBeingMarked = myLunchIndex;
            return false;
        }
        else if (oh.fish.getFishColor() == Color.cyan) {
            if (oh.previousNodeInGCTraversalIsAFish) {
                traverseBackFromGrayLine(oh.previousFishInGCTraversal);
                return false;
            }
            return true;
        }

        int mySnackIndex = gcHeap.getObjectPool(oh.objectPos + 2);

        if ((mySnackIndex != 0) && (oh.mySnackLineColor == Color.white)) {
            oh.mySnackLineColor = Color.gray;
            ObjectHandle mySnack = gcHeap.getObjectHandle(mySnackIndex);
            mySnack.previousNodeInGCTraversalIsAFish = true;
            mySnack.previousFishInGCTraversal = currentFishBeingMarked;
            if (mySnack.myColor == Color.white) {
                mySnack.myColor = Color.gray;
            }
            currentFishBeingMarked = mySnackIndex;
            return false;
        }
        else if (oh.previousNodeInGCTraversalIsAFish) {
            traverseBackFromGrayLine(oh.previousFishInGCTraversal);
            return false;
        }

        return true;
    }

    private void traverseBackFromGrayLine(int fishObjectHandle) {

        ObjectHandle oh = gcHeap.getObjectHandle(fishObjectHandle);

        int myFriendIndex = gcHeap.getObjectPool(oh.objectPos);

        if ((myFriendIndex != 0) && (oh.myFriendLineColor == Color.gray)) {
            ObjectHandle myFriend = gcHeap.getObjectHandle(myFriendIndex);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -