rookie
rookie

Reputation: 63

Stop one thread and start the other?

I am having trouble stopping one thread and starting another one. I have created two threads. One of them is first run from the main method. It then takes user input and if the desired input is provided, thread one (sound) is supposed to stop and start thread two (sound2). From the code below, thread two does start playing after the correct input is provided during thread one. But thread one does not stop. I can hear audio files from both thread one and thread two playing. Please help.

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

Upvotes: 2

Views: 678

Answers (1)

morgano
morgano

Reputation: 17422

Try stopping the clip when the thread is interrupted, adding clip.close() to the catch clause that catches the interruptedException, like this:

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

Upvotes: 4

Related Questions