Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,优雅地完成是什么意思? package com.madhubanti.singleprocess.player.interaction.main; 导入com.madhubanti.singleprocess.player.interaction.initiation.Initiator; 导入com.madhubanti.singleprocess.player.interaction.receiver.receiver; /** *启动应用程序,实例化播放器并帮助垃圾收集 * */ 公共类MainApp{ /** *main方法实例化发起方和接收方,并在消息中提供帮助 *他们之间的沟通 * *还将未使用的对象设置为null并调用System.exit * *@param args *字符串数组 */ 公共静态void main(字符串[]args){ //实例化启动器 启动器启动器层=新启动器(); //实例化接收方 Receiver Receiver Player=新接收器(); //开始发送消息 initiatorPlayer.sendMessage(receiverPlayer); //将未使用实例的值设置为null以进行垃圾回收 initiatorPlayer=null; receiverPlayer=null; 系统出口(0); } } 包com.madhubanti.singleprocess.player.interaction; 导入com.madhubanti.singleprocess.player.interaction.initiation.Initiator; /** *{@link Initiator}和{@link Receiver}的父类 * *@作者Madhubanti Jash * */ 公开课选手{ /** *返回字符串形式的计数器值 * *@param计数器 *1、2、3等 *@计数器的返回字符串格式 */ 公共字符串校验计数器(整数计数器){ 开关(计数器){ 案例1: 返回“第一”; 案例2: 返回“秒”; 案例3: 返回“第三”; 案例4: 返回“第四”; 案例5: 返回“第五”; 案例6: 返回“第六”; 案例7: 返回“第七”; 案例8: 返回“第八”; 案例9: 返回“第九”; 案例10: 返回“第十”; 违约: 返回“垃圾回收”; } } } 包com.madhubanti.singleprocess.player.interaction.initiation; 导入java.util.logging.Level; 导入java.util.logging.Logger; 导入com.madhubanti.singleprocess.player.interaction.player; 导入com.madhubanti.singleprocess.player.interaction.log.ConfigLogger; 导入com.madhubanti.singleprocess.player.interaction.receiver.receiver; /** *启动与{@link Receiver}的消息通信并接收新消息 *来自它的信息。 * *@作者Madhubanti Jash * */ 公共类启动器扩展了播放器{ /**初始化记录器*/ 私有静态最终记录器Logger=Logger.getLogger(Initiator.class.getName()); /** *向接收方发送消息并接收新消息作为回报 * *@param receiverPlayer *接收机实例 * */ 公共无效发送消息(接收者-接收者-播放器){ addLoggerHandler(记录器); 字符串消息=”; String prefix=“已为发送消息”; 字符串后缀=“时间”; for(int counter=1;counter_Java_Garbage Collection - Fatal编程技术网

在Java中,优雅地完成是什么意思? package com.madhubanti.singleprocess.player.interaction.main; 导入com.madhubanti.singleprocess.player.interaction.initiation.Initiator; 导入com.madhubanti.singleprocess.player.interaction.receiver.receiver; /** *启动应用程序,实例化播放器并帮助垃圾收集 * */ 公共类MainApp{ /** *main方法实例化发起方和接收方,并在消息中提供帮助 *他们之间的沟通 * *还将未使用的对象设置为null并调用System.exit * *@param args *字符串数组 */ 公共静态void main(字符串[]args){ //实例化启动器 启动器启动器层=新启动器(); //实例化接收方 Receiver Receiver Player=新接收器(); //开始发送消息 initiatorPlayer.sendMessage(receiverPlayer); //将未使用实例的值设置为null以进行垃圾回收 initiatorPlayer=null; receiverPlayer=null; 系统出口(0); } } 包com.madhubanti.singleprocess.player.interaction; 导入com.madhubanti.singleprocess.player.interaction.initiation.Initiator; /** *{@link Initiator}和{@link Receiver}的父类 * *@作者Madhubanti Jash * */ 公开课选手{ /** *返回字符串形式的计数器值 * *@param计数器 *1、2、3等 *@计数器的返回字符串格式 */ 公共字符串校验计数器(整数计数器){ 开关(计数器){ 案例1: 返回“第一”; 案例2: 返回“秒”; 案例3: 返回“第三”; 案例4: 返回“第四”; 案例5: 返回“第五”; 案例6: 返回“第六”; 案例7: 返回“第七”; 案例8: 返回“第八”; 案例9: 返回“第九”; 案例10: 返回“第十”; 违约: 返回“垃圾回收”; } } } 包com.madhubanti.singleprocess.player.interaction.initiation; 导入java.util.logging.Level; 导入java.util.logging.Logger; 导入com.madhubanti.singleprocess.player.interaction.player; 导入com.madhubanti.singleprocess.player.interaction.log.ConfigLogger; 导入com.madhubanti.singleprocess.player.interaction.receiver.receiver; /** *启动与{@link Receiver}的消息通信并接收新消息 *来自它的信息。 * *@作者Madhubanti Jash * */ 公共类启动器扩展了播放器{ /**初始化记录器*/ 私有静态最终记录器Logger=Logger.getLogger(Initiator.class.getName()); /** *向接收方发送消息并接收新消息作为回报 * *@param receiverPlayer *接收机实例 * */ 公共无效发送消息(接收者-接收者-播放器){ addLoggerHandler(记录器); 字符串消息=”; String prefix=“已为发送消息”; 字符串后缀=“时间”; for(int counter=1;counter

在Java中,优雅地完成是什么意思? package com.madhubanti.singleprocess.player.interaction.main; 导入com.madhubanti.singleprocess.player.interaction.initiation.Initiator; 导入com.madhubanti.singleprocess.player.interaction.receiver.receiver; /** *启动应用程序,实例化播放器并帮助垃圾收集 * */ 公共类MainApp{ /** *main方法实例化发起方和接收方,并在消息中提供帮助 *他们之间的沟通 * *还将未使用的对象设置为null并调用System.exit * *@param args *字符串数组 */ 公共静态void main(字符串[]args){ //实例化启动器 启动器启动器层=新启动器(); //实例化接收方 Receiver Receiver Player=新接收器(); //开始发送消息 initiatorPlayer.sendMessage(receiverPlayer); //将未使用实例的值设置为null以进行垃圾回收 initiatorPlayer=null; receiverPlayer=null; 系统出口(0); } } 包com.madhubanti.singleprocess.player.interaction; 导入com.madhubanti.singleprocess.player.interaction.initiation.Initiator; /** *{@link Initiator}和{@link Receiver}的父类 * *@作者Madhubanti Jash * */ 公开课选手{ /** *返回字符串形式的计数器值 * *@param计数器 *1、2、3等 *@计数器的返回字符串格式 */ 公共字符串校验计数器(整数计数器){ 开关(计数器){ 案例1: 返回“第一”; 案例2: 返回“秒”; 案例3: 返回“第三”; 案例4: 返回“第四”; 案例5: 返回“第五”; 案例6: 返回“第六”; 案例7: 返回“第七”; 案例8: 返回“第八”; 案例9: 返回“第九”; 案例10: 返回“第十”; 违约: 返回“垃圾回收”; } } } 包com.madhubanti.singleprocess.player.interaction.initiation; 导入java.util.logging.Level; 导入java.util.logging.Logger; 导入com.madhubanti.singleprocess.player.interaction.player; 导入com.madhubanti.singleprocess.player.interaction.log.ConfigLogger; 导入com.madhubanti.singleprocess.player.interaction.receiver.receiver; /** *启动与{@link Receiver}的消息通信并接收新消息 *来自它的信息。 * *@作者Madhubanti Jash * */ 公共类启动器扩展了播放器{ /**初始化记录器*/ 私有静态最终记录器Logger=Logger.getLogger(Initiator.class.getName()); /** *向接收方发送消息并接收新消息作为回报 * *@param receiverPlayer *接收机实例 * */ 公共无效发送消息(接收者-接收者-播放器){ addLoggerHandler(记录器); 字符串消息=”; String prefix=“已为发送消息”; 字符串后缀=“时间”; for(int counter=1;counter,java,garbage-collection,Java,Garbage Collection,优雅地结束通常意味着以一种可以调用拆卸/取消设置操作的方式结束。 为此,您必须确保应用程序不会因崩溃而终止 不需要优雅地完成 不需要将变量设置为null,因为JVM终止时所有Java对象都将被删除。因此,您应该删除这些语句 以及调用System.exit(0);,因为离开main() 优美的结尾很重要的示例 假设在您的代码中,Receiver在引擎盖下创建了一个未连接到JVM的资源,例如用于接收消息的网络套接字。 当程序终止时,您希望所有不再需要的资源都得到适当的处理:运行的JVM分配的对象,以

优雅地结束通常意味着以一种可以调用拆卸/取消设置操作的方式结束。
为此,您必须确保应用程序不会因崩溃而终止

不需要优雅地完成

不需要将变量设置为
null
,因为JVM终止时所有Java对象都将被删除。因此,您应该删除这些语句

以及调用
System.exit(0);
,因为离开
main()

优美的结尾很重要的示例

假设在您的代码中,
Receiver
在引擎盖下创建了一个未连接到JVM的资源,例如用于接收消息的网络套接字。
当程序终止时,您希望所有不再需要的资源都得到适当的处理:运行的JVM分配的对象,以及打开的套接字。
JVM退出将不必关闭连接到套接字的所有资源。调用
receiverPlayer.dipose()
可以实现这一点。 但要做到这一点,您需要让程序优雅地完成

例如:

  package com.madhubanti.singleprocess.player.interaction.main;

    import com.madhubanti.singleprocess.player.interaction.initiation.Initiator;
    import com.madhubanti.singleprocess.player.interaction.receiver.Receiver;

    /**
     * Starts application, Instantiate Players and help in garbage collection
     * 
     */
    public class MainApp {

        /**
         * main method instantiates Initiator and Receiver and helps in message
         * communication between them
         * 
         * Also sets unused objects to null and callss System.exit
         * 
         * @param args
         *            String array
         */
        public static void main(String[] args) {
            // Instantiates Initiator
            Initiator initiatorPlayer = new Initiator();
            // Instantiates Receiver
            Receiver receiverPlayer = new Receiver();
            // start sending message
            initiatorPlayer.sendMessage(receiverPlayer);
            // sets the value of the unused instances to null for garbage collection
            initiatorPlayer = null;
            receiverPlayer = null;
            System.exit(0);
        }

    }

    package com.madhubanti.singleprocess.player.interaction;

    import com.madhubanti.singleprocess.player.interaction.initiation.Initiator;

    /**
     * Parent class of {@link Initiator} and {@link Receiver}
     * 
     * @author Madhubanti Jash
     * 
     */
    public class Player {

        /**
         * returns counter value in string
         * 
         * @param counter
         *            1,2,3 etc
         * @return String format of counter
         */
        public String checkCounter(int counter) {
            switch (counter) {
            case 1:
                return "first";
            case 2:
                return "second";
            case 3:
                return "third";
            case 4:
                return "fourth";
            case 5:
                return "fifth";
            case 6:
                return "sixth";
            case 7:
                return "seventh";
            case 8:
                return "eighth";
            case 9:
                return "ninth";
            case 10:
                return "tenth";
            default:
                return "garbage collection";
            }
        }

    }

    package com.madhubanti.singleprocess.player.interaction.initiation;

    import java.util.logging.Level;
    import java.util.logging.Logger;

    import com.madhubanti.singleprocess.player.interaction.Player;
    import com.madhubanti.singleprocess.player.interaction.log.ConfigLogger;
    import com.madhubanti.singleprocess.player.interaction.receiver.Receiver;

    /**
     * Initiates message communication with {@link Receiver} and receives new
     * message from it.
     * 
     * @author Madhubanti Jash
     * 
     */
    public class Initiator extends Player {

        /** initializes logger */
        private static final Logger LOGGER = Logger.getLogger(Initiator.class.getName());

        /**
         * sends message to Receiver and receives new message in return
         * 
         * @param receiverPlayer
         *            instance of Receiver
         * 
         */
        public void sendMessage(Receiver receiverPlayer) {
            ConfigLogger.addLoggerHandler(LOGGER);
            String message = "";
            String prefix = "Sent message for ";
            String postfix = " Time";
            for (int counter = 1; counter <= 10; counter++) {
                message = prefix + super.checkCounter(counter) + postfix;
                LOGGER.log(Level.FINER, "message to be sent is: {0}", message);
                String newReceivedMessage = receiverPlayer.receiveMessage(message, counter);
                LOGGER.log(Level.FINER, "Received new message from Receiver is: {0}", newReceivedMessage);
            }
        }
    }

    package com.madhubanti.singleprocess.player.interaction.receiver;

    import com.madhubanti.singleprocess.player.interaction.Player;
    import com.madhubanti.singleprocess.player.interaction.initiation.Initiator;

    /**
     * Receives message communication from {@link Initiator} and sends back new
     * message
     * 
     * @author Madhubanti Jash
     * 
     */
    public class Receiver extends Player {

        /**
         * receives message from initiator and sends back new message in return
         * 
         * @param message
         *            received message
         * @return newMessage new message to Initiator
         * 
         */
        public String receiveMessage(String message, int counter) {
            String messageForNullValue = "have not received message; so not returning new message.";
            String postfix = " time from Receiver class.";
            String receivedMessage = message;
            String prefix = "Received message for ";
            String checkCounter = checkCounter(counter);
            String counterString = " The counter is: ";
            String newMessage = receivedMessage == null ? messageForNullValue
                    : prefix + checkCounter + postfix + counterString + counter;
            return newMessage;
        }

    }

    package com.madhubanti.singleprocess.player.interaction.log;

    import java.util.logging.ConsoleHandler;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /** responsible to add log level and handler */
    public class ConfigLogger extends Logger {

        /**
         * parameterized constructor
         * 
         * @param name
         *            A name for the logger
         * @param resourceBundleName
         *            name of resource bundle
         * 
         */
        protected ConfigLogger(String name, String resourceBundleName) {
            super(name, resourceBundleName);
        }

        /**
         * add log level and handler
         * 
         * @param logger
         *            LOGGER
         */
        public static void addLoggerHandler(Logger logger) {
            ConsoleHandler consoleHandler = new ConsoleHandler();
            logger.addHandler(consoleHandler);
            consoleHandler.setLevel(Level.ALL);
            logger.setLevel(Level.ALL);
        }
    }

不,你不需要在星空中做任何事情。你根本不需要做任何事情。你认为finalize意味着什么?为了回答这个问题,你需要了解这一点。不过,一般来说,你有3行代码以多种方式使你的程序不那么优雅,应该删除而不是替换。这不是为什么“在终结时”指的是。@Madhubanti不必担心。您需要重新检查您的假设。您似乎认为终结是关于请求垃圾回收的,但事实并非如此。错误的前提将导致错误的结论。在尝试编写优美的终结器之前,请阅读终结器是什么。终结器的存在是为了帮助您清理垃圾p不由GC处理的资源。它们允许类指定在收集该类的实例时可能运行的一些任意代码。这将GC管理的资源和非GC管理的资源的生存期联系在一起。这与将变量设置为
null
无关。请搜索Java终结器,您将你能提供一个例子吗?我加了一个具体的例子example@Madhubanti不客气:)不,你不需要
public static void main(String[] args)  {
  Receiver receiverPlayer = null;
  try{
       // Instantiates Initiator
          Initiator initiatorPlayer = new Initiator();
       // Instantiates Receiver
          receiverPlayer = new Receiver();
       // start sending message
         initiatorPlayer.sendMessage(receiverPlayer); 
     }
  finally{
       if (receiverPlayer != null){
          receiverPlayer.dispose(); 
       }
  }
}