【问题标题】:Closing Stream by terminating program - Common practice?通过终止程序关闭流 - 常见做法?
【发布时间】:2015-09-18 08:12:17
【问题描述】:

我有一个实现Runnable 的音频播放器。它开始一个声音,然后终止。这是一种常见的做法,还是我应该自己关闭它,就像在最后一种方法中一样,目前没有使用。在我看来,让它终止并自动强制关闭其余部分是个好主意。

public class AudioPlayer implements Runnable {

    AudioInputStream audioIn;
    Clip clip;

    public AudioPlayer (String res) {

        try {
            URL url = this.getClass().getResource(res);
            audioIn = AudioSystem.getAudioInputStream(url);
            clip = AudioSystem.getClip();
            clip.open(audioIn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        clip.start();
    }

    public void close() throws IOException {
        try {
            clip.close();
            audioIn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 不要在不再需要资源时立即明确释放它们!
  • 您是否打算在AudioPlayer 的单个实例上多次调用run()?如果没有,您可以/应该在 run() 方法中包含对 close() 的调用。
  • @GhostCat :P...
  • 只是想知道:不接受的原因是什么?
  • @GhostCat 我看到你删除了所有的 cmets,所以我很好奇如果我删除接受你会怎么做,哈哈。对不起。没用的“社会实验”。 B-)

标签: java io stream


【解决方案1】:

要么在 run() 方法中打开流并在 finally 子句中关闭它们,要么实现 AutoCloseable 以便您的类可以用作资源。

【讨论】:

    【解决方案2】:

    直接回答您的问题:不,这不是常见做法,而是不好的做法!

    一般来说,获取资源而不明确释放它们是不好的做法。特别是对于流 - 后面可能有文件句柄,各种各样的东西。只是打开它们并扔掉它们可能会起作用;但正如所说:只是不好的做法。请注意:对于任何打算运行更长时间的程序......释放资源不仅“好”,而且绝对必须这样做。

    尤其是考虑到 Java 7 几年前引入了 try-with-resources 时。

    【讨论】:

      【解决方案3】:

      我建议在使用后立即释放内存/资源,为此,存在 finally 块:

      public AudioPlayer (String res) {
          try {
              URL url = this.getClass().getResource(res);
              audioIn = AudioSystem.getAudioInputStream(url);
              clip = AudioSystem.getClip();
              clip.open(audioIn);
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              close();
          }
      }
      

      但是,如果您的音频流在完成后自动关闭,则无需在没有错误的情况下强制关闭:

      public AudioPlayer (String res) {
          try {
              URL url = this.getClass().getResource(res);
              audioIn = AudioSystem.getAudioInputStream(url);
              clip = AudioSystem.getClip();
              clip.open(audioIn);
          } catch (Exception e) {
              e.printStackTrace();
              close();
          }
      }
      

      【讨论】:

        【解决方案4】:

        请注意:为了更加确保清理所有内容,您可能希望这样写:

        public void close() throws IOException {
            try {
                clip.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        
            try {
                audioIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-26
          • 2011-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-30
          • 1970-01-01
          • 2021-07-07
          相关资源
          最近更新 更多