Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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 通过终止程序关闭流-常见做法?_Java_Io_Stream - Fatal编程技术网

Java 通过终止程序关闭流-常见做法?

Java 通过终止程序关闭流-常见做法?,java,io,stream,Java,Io,Stream,我有一个音频播放器,它实现了Runnable。它开始发出声音,然后结束。这是一种常见的做法还是我应该在事后自己关闭它,就像在最后一种方法中一样,这是目前没有使用的方法。在我看来,让它终止并强制自动关闭其余部分是个好主意 public class AudioPlayer implements Runnable { AudioInputStream audioIn; Clip clip; public AudioPlayer (String res) {

我有一个音频播放器,它实现了
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();
        }
    }
}

直接回答你的问题:不,那不是普通的练习,而是糟糕的练习

一般来说,获取资源而不显式地释放资源是不好的做法。特别是对于流,可能有文件句柄在后面,各种各样的东西。只要打开它们并扔掉它们就行了;但正如所说:这只是一种坏习惯。注意:对于任何打算运行更长时间的程序。。。释放资源不仅是“好的”,而且是绝对必须这样做的


特别是考虑到Java7在几年前就引入了try-with-resources

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

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();
    }
}

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

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

    try {
        audioIn.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

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

一旦不再需要资源,就显式释放资源!是否要在
AudioPlayer
的单个实例上多次调用
run()
?如果没有,你可以/应该在
run()
方法中包含对
close()
的调用。@GhostCat:P…只是想知道:取消接受的原因是什么?@GhostCat我看到你删除了所有的评论,所以我很好奇如果我删除接受,你会怎么做,哈哈。对不起。无用的“社会实验”。B-)