停止一个线程并启动另一个线程?

时间:2015-07-29 01:55:32

标签: java multithreading interrupt

我无法停止一个线程并启动另一个线程。我创建了两个线程。其中一个是首先从main方法运行。然后它接受用户输入,如果提供了所需的输入,则线程1(声音)应该停止并启动线程2(sound2)。从下面的代码开始,在第一个线程中提供正确的输入后,线程2会开始播放。但线程一不会停止。我可以听到来自第一线和第二线的音频文件播放。请帮忙。

public class crytask {

public static void main(String args[]) throws InterruptedException {
    Runnable sound = new sound();
    final Thread soundthread = new Thread(sound);

    soundthread.start();
    int count = 0;
        System.out.println("Enter");
        Scanner reader = new Scanner(System.in);
        int a = reader.nextInt();
        if (a==12)
        soundthread.interrupt();
        System.out.println("this thread is cancelled");

    Runnable sound2 = new sound2();
    final Thread soundthread2 = new Thread(sound2);
    soundthread2.start();
    int count2 = 0;
    System.out.println("Enter");
    Scanner reader2 = new Scanner(System.in);
    int b = reader2.nextInt();
    if (b==12)
    soundthread2.interrupt();

}}


class sound implements Runnable {
@Override
public void run() {     
    try {       
        AudioInputStream audioInputStream =       AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
        Thread.sleep(clip.getMicrosecondLength() / 1000);
    } catch(InterruptedException ex) {
        System.out.println("Cancelled 1!");
    } catch (Exception ex) {
        System.out.println("Error with playing sound.");
        ex.printStackTrace();
    }
}
}

class sound2 implements Runnable {
@Override
public void run() {     
    try {       
        AudioInputStream audioInputStream2 = AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream2);
        clip.start();
        Thread.sleep(clip.getMicrosecondLength() / 1000);
    } catch(InterruptedException ex) {
        System.out.println("Cancelled 2!");
    } catch (Exception ex) {
        System.out.println("Error with playing sound.");
        ex.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:4)

尝试在线程中断时停止剪辑,将clip.close()添加到catch子句中以捕获interruptedException,如下所示:

class sound implements Runnable {

    @Override
    public void run() {
        Clip clip = null; // take the declaration of the clip variable out of the try - catch
        try {       
            AudioInputStream audioInputStream =
                   AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
            clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
            Thread.sleep(clip.getMicrosecondLength() / 1000);
        } catch(InterruptedException ex) {
            clip.stop(); // <--- ADD THIS LINE
            System.out.println("Cancelled 1!");
        } catch (Exception ex) {
            System.out.println("Error with playing sound.");
            ex.printStackTrace();
        }
    }
}