Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Multithreading_Thread Sleep - Fatal编程技术网

Java 线程中断不会立即停止程序

Java 线程中断不会立即停止程序,java,multithreading,thread-sleep,Java,Multithreading,Thread Sleep,我用sun.audio创建了一个声音类。(我知道它已被弃用,请不要客气。) 由于某种原因,无论我做了什么尝试,线程在应该停止的时候都没有完全停止 这是我的声音课: package bob.classes; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import ja

我用sun.audio创建了一个声音类。(我知道它已被弃用,请不要客气。)

由于某种原因,无论我做了什么尝试,线程在应该停止的时候都没有完全停止

这是我的声音课:

package bob.classes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

/*
 * Custom class for playing audio (audio file must be in src)
 */
public class Sound {
    private ContinuousAudioDataStream loop = null; //Puts music data into loop
    private InputStream in = null; //Takes in music file as input
    private AudioStream audioStreamMusic = null; //Takes in InputStream of music file as input
    private AudioData audioData = null; //Changes AudioStream to data
    private boolean loopable; //Whether the music input is meant to be looped or not
    private String music; //The name of the music file
    private double length; //The duration of the audio snippet in seconds

    /*
     * Takes in music file name and whether or not the file is loopable
     */
    public Sound(String music, boolean loopable) throws IOException, UnsupportedAudioFileException {
        if(music.length() < 4 || !music.substring(music.length() - 4, music.length()).equals(".wav"))
            throw new IOException(music + " (Given file must be .wav)");
        else if(new File(music).length() > 1000000)
            throw new IOException(music + " (Given file must not be over 1 megabyte)");
        this.music = System.getProperty("user.dir") + "/" + music;
        this.loopable = loopable;
        in = new FileInputStream(music);
        audioStreamMusic = new AudioStream(in);
        if(loopable) {
            audioData = audioStreamMusic.getData();
            loop = new ContinuousAudioDataStream(audioData);
        }
        AudioInputStream stream = AudioSystem.getAudioInputStream(new File(music));
        AudioFormat format = stream.getFormat();
        long frames = stream.getFrameLength();
        length = (double) frames/format.getFrameRate();
    }

    /*
     * Also takes in whether the file size limit should be ignored
     */
    public Sound(String music, boolean loopable, boolean override) throws IOException {
        if(music.length() < 4 || !music.substring(music.length() - 4, music.length()).equals(".wav"))
            throw new IOException(music + " (Given file must be .wav)");
        if(!override) {
            if(new File(music).length() > 1000000)
                throw new IOException(music + " (Given file must not be over 1 megabyte)");
        }
        this.music = System.getProperty("user.dir") + "/" + music;
        this.loopable = loopable;
        in = new FileInputStream(music);
        audioStreamMusic = new AudioStream(in);
        if(loopable) {
            audioData = audioStreamMusic.getData();
            loop = new ContinuousAudioDataStream(audioData);
        }
    }

    /*
     * Plays audio file
     */
    public void play() {
        if(loopable)
            AudioPlayer.player.start(loop);
        else {
            try {
                in = new FileInputStream(music);
                audioStreamMusic = new AudioStream(in);
                AudioPlayer.player.start(audioStreamMusic);
            }
            catch(IOException error) {
                System.out.println(error);
            }
        }
    }

    /*
     * Stops audio file if playing
     */
    public void stop() {
        if(loopable)
            AudioPlayer.player.stop(loop);
        else
            AudioPlayer.player.stop(audioStreamMusic);
    }

    /*
     * Returns a string representation of the sound, including
     * the given name of the audio file as well as whether or
     * not it is loopable
     */
    @Override
    public String toString() {
        return String.format("Sound[music = %s, loopable = %b]", music, loopable);
    }

    public boolean equals(Sound other) {
        if(music.equals(other.music))
            return true;
        return false;
    }

    /*
     * Returns value of music
     */
    public String getMusic() {
        return music;
    }

    /*
     * Returns value of loopable
     */
    public boolean getLoopable() {
        return loopable;
    }

    /*
     * Returns value of length
     */
    public double getLength() {
        return length;
    }

    /*
     * Plays group of Sounds
     */
    public static Thread playGroup(Sound[] sounds, boolean loopable) throws InterruptedException {
        return playGroup(new ArrayList<Sound>(Arrays.asList(sounds)), loopable);
    }

    /*
     * Plays group of Sounds
     */
    public static Thread playGroup(ArrayList<Sound> sounds, boolean loopable) throws InterruptedException {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                do {
                    int i = 0;
                    try {
                        sounds.get(0).play();
                        Thread.sleep((long) (sounds.get(0).getLength() * 1000));
                        for(i = 1; i < sounds.size(); ++i) {
                            sounds.get(i).play();
                            sounds.get(i - 1).stop();
                            Thread.sleep((long) (sounds.get(i).getLength() * 1000));
                        }
                    }
                    catch(Exception e) {
                        sounds.get(i).stop();
                        break;
                    }
                } while(loopable);
            }
        };
        Thread t = new Thread(r);
        t.start();
        return t;
    }

    public static void stopGroup(Thread t) {
        while(t != null && !t.isInterrupted()) {
            System.out.println("Test");
            t.interrupt();
        }
    }

}
包bob.classes;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.array;
导入javax.sound.sampled.AudioFormat;
导入javax.sound.sampled.AudioInputStream;
导入javax.sound.sampled.AudioSystem;
导入javax.sound.sampled.unsupportDaudioFileException;
导入sun.audio.AudioData;
导入sun.audio.AudioPlayer;
导入sun.audio.AudioStream;
导入sun.audio.ContinuousAudioDataStream;
/*
*用于播放音频的自定义类(音频文件必须在src中)
*/
公共级音响{
private ContinuousAudioDataStream loop=null;//将音乐数据放入循环中
private InputStream in=null;//接收音乐文件作为输入
private AudioStream audioStreamMusic=null;//接收音乐文件的InputStream作为输入
私有AudioData AudioData=null;//将AudioStream更改为数据
private boolean loopable;//音乐输入是否要循环
私有字符串音乐;//音乐文件的名称
private double length;//音频片段的持续时间(秒)
/*
*接收音乐文件名以及该文件是否可循环
*/
公共声音(字符串音乐,布尔循环)引发IOException,UnsupportdAudioFileException{
if(music.length()<4 | |!music.substring(music.length()-4,music.length()).equals(“.wav”))
抛出新IOException(music+“(给定文件必须是.wav)”);
else if(新文件(music).length()>1000000)
抛出新IOException(music+(给定文件不得超过1兆字节));
this.music=System.getProperty(“user.dir”)+“/”music;
this.loopable=可循环;
in=新文件输入流(音乐);
audioStreamMusic=新的音频流(in);
if(可循环){
audioData=audioStreamMusic.getData();
loop=新的连续音频数据流(音频数据);
}
AudioInputStream=AudioSystem.getAudioInputStream(新文件(音乐));
AudioFormat=stream.getFormat();
长帧=stream.getFrameLength();
长度=(双)帧/format.getFrameRate();
}
/*
*还考虑是否应忽略文件大小限制
*/
公共声音(字符串音乐、布尔循环、布尔覆盖)引发IOException{
if(music.length()<4 | |!music.substring(music.length()-4,music.length()).equals(“.wav”))
抛出新IOException(music+“(给定文件必须是.wav)”);
如果(!覆盖){
if(新文件(music).length()>1000000)
抛出新IOException(music+(给定文件不得超过1兆字节));
}
this.music=System.getProperty(“user.dir”)+“/”music;
this.loopable=可循环;
in=新文件输入流(音乐);
audioStreamMusic=新的音频流(in);
if(可循环){
audioData=audioStreamMusic.getData();
loop=新的连续音频数据流(音频数据);
}
}
/*
*播放音频文件
*/
公共游戏{
if(可循环)
AudioPlayer.player.start(循环);
否则{
试一试{
in=新文件输入流(音乐);
audioStreamMusic=新的音频流(in);
AudioPlayer.player.start(audioStreamMusic);
}
捕获(IOException错误){
系统输出打印项次(错误);
}
}
}
/*
*播放时停止音频文件
*/
公共停车场(){
if(可循环)
音频播放器。播放器。停止(循环);
其他的
AudioPlayer.player.stop(audioStreamMusic);
}
/*
*返回声音的字符串表示形式,包括
*音频文件的给定名称,以及是否
*不是,它是可循环的
*/
@凌驾
公共字符串toString(){
返回String.format(“声音[music=%s,loopable=%b]”,music,loopable);
}
公共布尔等于(声音其他){
如果(音乐等于(其他音乐))
返回true;
返回false;
}
/*
*返回音乐的价值
*/
公共字符串getMusic(){
回归音乐;
}
/*
*返回loopable的值
*/
公共布尔getLoopable(){
返回循环;
}
/*
*返回长度的值
*/
公共双getLength(){
返回长度;
}
/*
*播放一组声音
*/
公共静态线程播放组(Sound[]sounds,boolean loopable)抛出InterruptedException{
返回播放组(新的ArrayList(Arrays.asList(sounds)),可循环);
}
/*
*播放一组声音
*/
公共静态线程播放组(ArrayList声音,布尔循环)抛出InterruptedException{
Runnable r=新的Runnable(){
@凌驾
公开募捐{
做{
int i=0;
试一试{
sounds.get(0.play();
sleep((长)(sounds.get(0.getLength()*1000));
对于(i=1;ipackage bob.classes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.sound.sampled.UnsupportedAudioFileException;

public class Test {

    public static void main(String[] args) throws InterruptedException, IOException, UnsupportedAudioFileException {
        Thread t = Sound.playGroup(new ArrayList<Sound>(Arrays.asList(new Sound("music.wav", false))), false);
        Thread.sleep(1000);
        Sound.stopGroup(t);
        Thread t2 = Sound.playGroup(new ArrayList<Sound>(Arrays.asList(new Sound("music2.wav", false))), false);
        Thread.sleep(2000);
        Sound.stopGroup(t2);
        //The program does not end right away here.
    }

}
  public static void stopGroup(Thread t) {
        while(t != null && !t.isInterrupted()) {
            System.out.println("Test");
            t.interrupt();
            isInterrupted = true;
        }
    }
do {
  ...
} while(loopable && isInterrupted);
while(loopable && !isInterrupted);
while (!Thread.currentThread().isInterrupted())