Java 停止一个线程,启动另一个线程?
我无法停止一个线程并启动另一个线程。我已经创建了两个线程。其中一个是第一次从main方法运行。然后它接受用户输入,如果提供了所需的输入,线程1(声音)应该停止并启动线程2(声音2)。从下面的代码中可以看出,在线程1期间提供了正确的输入之后,线程2确实开始播放。但是线程一并没有停止。我可以听到线程1和线程2播放的音频文件。请帮忙Java 停止一个线程,启动另一个线程?,java,multithreading,interrupt,Java,Multithreading,Interrupt,我无法停止一个线程并启动另一个线程。我已经创建了两个线程。其中一个是第一次从main方法运行。然后它接受用户输入,如果提供了所需的输入,线程1(声音)应该停止并启动线程2(声音2)。从下面的代码中可以看出,在线程1期间提供了正确的输入之后,线程2确实开始播放。但是线程一并没有停止。我可以听到线程1和线程2播放的音频文件。请帮忙 public class crytask { public static void main(String args[]) throws InterruptedExce
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();
}
}
}
尝试在线程中断时停止剪辑,将
clip.close()
添加到捕获interruptedException的catch子句中,如下所示:
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();
}
}
}
类声音实现可运行{
@凌驾
公开募捐{
Clip Clip=null;//将Clip变量的声明从try-catch中取出
试试{
音频输入流音频输入流=
getAudioInputStream(新文件(“/Users/babe/Desktop/Con1.wav”).getAbsoluteFile());
clip=AudioSystem.getClip();
clip.open(音频输入流);
clip.start();
Thread.sleep(clip.getMicrosecondLength()/1000);
}捕获(中断异常例外){
clip.stop();//您有:if(a==12)
在中断之前,a==12
是否返回true?是的。我输入了12,它似乎向前移动到sound2,但sound1继续播放声音。仅仅因为您继续移动并不意味着这是真的,受if语句影响的唯一代码位是soundthread.interrupt()
你怎么知道线程一没有停止?@immibis因为我仍然可以听到线程一播放的声音。这是一个正确的答案。Clip
类在调用start()时不会阻塞
,它会立即返回-它不需要调用线程继续存在/执行以进行播放。因此,当您希望停止播放时,需要手动停止剪辑。