在run()方法之后,线程继续运行 [英] Thread continues to run after run() method

查看:83
本文介绍了在run()方法之后,线程继续运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在游戏中播放声音时遇到问题.当处理声音播放的线程退出时,它的run方法不会终止/结束/停止.我知道正是这种方法引起了问题,因为当我注释掉整个内容时,不再创建线程. (已通过JVisualVM检查).问题在于退出运行方法后线程不会终止.我已经放置了一条打印命令,以确保它实际上到达run()方法的末尾,并且总是如此.

I have an issue with playing sound in my game. When the Thread that handles the sound playback exits it's run method it doesn't terminate/end/stop. I know it's this method that causes the problem, since when I comment the whole thing away no more Threads get created. (Checked with JVisualVM). The problem is that Threads do not get terminated after exiting the run method. I've placed a print command to ensure that it actually reaches the end of the run() method, and it always does.

但是,当我用JVisualVM检查进程时,播放的每种声音的线程数都会增加1.我还注意到,每播放一个声音,守护进程线程的数量就会增加1.我不确定什么是守护线程以及它们如何工作,但是我已经尝试了多种方法来杀死线程.包括Thread.currentThread .stop() .destroy() .suspend() .interrupt()并通过run()方法返回;

However, when I check the process with JVisualVM, the thread count grows by 1 for each sound played. I also noted that the number of daemon threads is increased by 1 for each sound played. I am not sure what daemon threads are and how they work, but I've tried to kill the Thread in a number of ways. Including Thread.currentThread .stop() .destroy() .suspend() .interrupt() and returning from the run() method by return;

在编写此消息时,我意识到我需要关闭clip对象.这导致没有额外的线程被创建和维持.但是,现在声音有时消失了,我不知道为什么.现在,我可以选择并行播放声音,或者看到无数线程使我的cpu过载,或者在播放新声音时突然结束声音.

While writing this message I realised I need to close the clip object. This resulted in no extra threads being created and sustained. However, now the sound sometimes disappears and I have no idea why. Right now, I can choose between having sound in parallel and see my cpu get overloaded by an endless number of threads or have the sounds end abruptly whenever a new sound is played.

如果有人知道并行播放多种声音的另一种方法,或者知道我的代码出了什么问题,我将不胜感激.

If anyone knows of a different approach of playing multiple sounds in parallel or knows what's wrong with my code, I would greatly appreciate any help.

这是方法:

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

推荐答案

关于Java线程的一些事情:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆