线程在运行run()方法后仍然继续运行。

3
我在游戏中播放声音时遇到了问题。当处理声音播放的线程退出其运行方法时,它不会终止/结束/停止。我知道这个方法是导致问题的原因,因为当我将整个内容注释掉后就不再创建任何线程了(使用JVisualVM进行了检查)。问题在于线程在退出run方法后不会被终止。我已经放置了一个打印命令来确保它实际上到达了run()方法的末尾,并且它总是会到达。
然而,当我使用JVisualVM检查进程时,每次播放声音时线程计数都会增加1。我还注意到每次播放声音时守护线程的数量会增加1。我不确定守护线程是什么以及它们如何工作,但我已经尝试了多种方式来终止线程,包括Thread.currentThread.stop()、.destroy()、.suspend()、.interrupt()以及通过return;从run()方法返回。
在编写此消息时,我意识到需要关闭剪辑对象。这样就不会创建和维持任何额外的线程。然而,现在声音有时会消失,我不知道为什么。现在,我可以选择在并行中播放声音并看到我的CPU由无数线程超载,或者在播放新声音时突然结束声音。
如果有人知道在并行中播放多个声音的不同方法或知道我的代码有什么问题,我将非常感激任何帮助。
这是该方法:
public static synchronized void playSound(final String folder, final String name) {
        new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
            @Override
            public void run() {
                Clip clip = null;
                AudioInputStream inputStream = null;
                try{
                    do{
                        if(clip == null || inputStream == null)
                                clip = AudioSystem.getClip();
                                inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                        if(clip != null && !clip.isActive())
                                inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                                clip.open(inputStream);
                                clip.start(); 
                    }while(clip.isActive());
                    inputStream.close();
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                } catch (UnsupportedAudioFileException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

3
  1. 你确定你的线程已经执行完了 run() 方法吗?也许 AudioSystem 使用一些内部线程来完成工作,并通过这种方式防止你的线程结束。
  2. 你确定 if 语句中没有漏掉括号吗?代码缩进并不能反映出代码块。
- mschenk74
4
是的,在if语句中缺少一个{,意思是音频流一直处于打开状态...稍后剪辑会被打开,无论剪辑是否处于活动状态。 - xtraclass
1
你知道在Java中缩进是没有意义的吧?创建一个块(一串语句)的唯一方法是使用花括号 { ... } - erickson
你的代码看起来很好,只是有几个问题:clip.isActive 是什么意思?另外,你尝试过 Thread.join() 吗? - Sachin Thapa
2个回答

9

关于Java线程的几点说明:

  • 一个线程在退出其Run()方法时总是会死亡。在您的情况下,其他线程是在您调用的方法内创建的,但您的线程结束了(您可以通过给它命名并查看它何时死亡来检查它)。
  • 永远不要使用.stop()、.destroy()或.suspend()方法杀死线程。这些方法已经过时,不应再使用。相反,您应该基本上到达Run()方法的结尾。这就是Thread.interrupt()的作用,但您必须通过检查Thread.isInterrupted()标志并抛出InterruptedException并处理它来支持中断您的线程(有关更多详细信息,请参见如何停止线程)。
  • “守护线程是一种线程,它不会阻止JVM在程序完成时退出,但线程仍在运行”

关于您的代码的几点说明:

  • 您缺少花括号,正如许多用户所提到的那样
  • 我不太理解您试图实现什么,但do-while循环似乎是多余的。有其他好的方法来等待声音播放完成(如果这是您的目标),而循环不是其中之一。没有Sleep的while循环会无缘无故地消耗您的CPU。
  • 您应该Close()(和Stop())Clip,如您所提到的,以释放系统资源。

带有调试注释的工作示例:

尝试运行此代码,并查看它是否符合您的要求。我为您添加了一些线程方法调用和一些System.out.print,以便您在每个代码块发生时查看。尝试使用tryToInterruptSound和mainTimeOut来查看它们如何影响输出。

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class PlaySound {
    private static boolean tryToInterruptSound = false;
    private static long mainTimeOut = 3000;
    private static long startTime = System.currentTimeMillis();

    public static synchronized Thread playSound(final File file) {

        Thread soundThread = new Thread() {
            @Override
            public void run() {
                try{
                    Clip clip = null;
                    AudioInputStream inputStream = null;
                    clip = AudioSystem.getClip();
                    inputStream = AudioSystem.getAudioInputStream(file);
                    AudioFormat format = inputStream.getFormat();
                    long audioFileLength = file.length();
                    int frameSize = format.getFrameSize();
                    float frameRate = format.getFrameRate();
                    long durationInMiliSeconds = 
                            (long) (((float)audioFileLength / (frameSize * frameRate)) * 1000);

                    clip.open(inputStream);
                    clip.start();
                    System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound started playing!");
                    Thread.sleep(durationInMiliSeconds);
                    while (true) {
                        if (!clip.isActive()) {
                            System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound got to it's end!");
                            break;
                        }
                        long fPos = (long)(clip.getMicrosecondPosition() / 1000);
                        long left = durationInMiliSeconds - fPos;
                        System.out.println("" + (System.currentTimeMillis() - startTime) + ": time left: " + left);
                        if (left > 0) Thread.sleep(left);
                    }
                    clip.stop();  
                    System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound stoped");
                    clip.close();
                    inputStream.close();
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                } catch (UnsupportedAudioFileException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound interrupted while playing.");
                }
            }
        };
        soundThread.setDaemon(true);
        soundThread.start();
        return soundThread;
    }

    public static void main(String[] args) {
        Thread soundThread = playSound(new File("C:\\Booboo.wav"));
        System.out.println("" + (System.currentTimeMillis() - startTime) + ": playSound returned, keep running the code");
        try {   
            Thread.sleep(mainTimeOut );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (tryToInterruptSound) {
            try {   
                soundThread.interrupt();
                Thread.sleep(1); 
                // Sleep in order to let the interruption handling end before
                // exiting the program (else the interruption could be handled
                // after the main thread ends!).
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("" + (System.currentTimeMillis() - startTime) + ": End of main thread; exiting program " + 
                (soundThread.isAlive() ? "killing the sound deamon thread" : ""));
    }
}
  • playSound 运行在一个后台线程上,因此当主线程(唯一的非后台线程)结束时,它也会停止。
  • 我根据 这位开发者的方法 计算了声音文件的长度,以便我提前知道需要播放 Clip 的时间。这样可以让线程 Sleep() 并且不使用 CPU。我还使用了额外的 isActive() 来测试是否真正结束,如果没有 - 就计算剩余时间并再次 Sleep()(由于两个原因,声音可能仍在播放:1. 长度计算未考虑微秒,2. “您不能假设调用 sleep 将精确地暂停线程指定的时间段”)。

1

你的代码实际上是这样的

public static synchronized void playSound(final String folder, final String name) {
    new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
        @Override
        public void run() {
            Clip clip = null;
            AudioInputStream inputStream = null;
            try{
                do{
                    if(clip == null || inputStream == null){
                            clip = AudioSystem.getClip();
                    }
                    inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                    if(clip != null && !clip.isActive()){
                            inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                    }
                    clip.open(inputStream);
                    clip.start(); 
                }while(clip.isActive());
                inputStream.close();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

if语句仅对其后的第一个命令起作用。第二个“if”是无意义的,因为该语句已经运行。在我的看来,每次循环执行时,剪辑都会再次“.start”,无论剪辑是否处于活动状态,都会生成另一个线程。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接