Thread not interrupting even though I#39;m calling thread.interrupt()(即使我正在调用 thread.interrupt(),线程也不会中断)
问题描述
我正在学习如何在 Android 中使用线程,为此我制作了一个播放一系列音符的小应用程序.这个想法是有一个开始按钮和一个结束按钮,并且(显然)如果你按下开始按钮,它就会开始播放音乐,如果你按下结束按钮,它就会停止.开始按钮工作得很好,但问题是结束按钮没有.我很难弄清楚原因,所以也许你们中的一些人可以帮助我.这是代码:
I'm learning how to use threads in Android, and to do that I've made a small application that plays a series of notes. The idea is that there is a start button and an end button and that (obviously) if you press the start button it starts playing music, and if you press the end button, it stops. The start button works just fine, but the problem is that the end button doesn't. I'm having trouble figuring out why, so maybe some of you can help me out. This is the code:
public class PressAndPlay extends Activity {
private volatile Thread initBkgdThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// create thread
initBkgdThread = new Thread(new Runnable() {
public void run() {
play_music();
}
});
initBkgdThread.start();
}
});
Button endButton = (Button) findViewById(R.id.end);
endButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
end_music();
}
});
}
int[] notes = {R.raw.c5, R.raw.b4, R.raw.a4, R.raw.g4};
int NOTE_DURATION = 400;
MediaPlayer m_mediaPlayer;
private void play_music() {
for(int ii=0; ii<12; ii++) {
//check to ensure main activity is not paused
if(!paused) {
if (m_mediaPlayer != null) {m_mediaPlayer.release();}
m_mediaPlayer = MediaPlayer.create(this, notes[ii%4]);
m_mediaPlayer.start();
try {
Thread.sleep(NOTE_DURATION);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void end_music() {
if(initBkgdThread != null) {
initBkgdThread.interrupt();
initBkgdThread = null;
}
}
boolean paused = false;
@Override
protected void onPause() {
paused = true;
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
paused = false;
}
}
推荐答案
您正在播放线程上调用 interrupt()
但它可能正在等待 sleep
在时间.这将捕获睡眠以抛出 InterruptedException
.您需要捕获该异常并退出循环以停止播放:
You are calling interrupt()
on the playing thread but it is probably waiting in sleep
at the time. This will caught sleep to throw a InterruptedException
. You need to catch that exception and exit the loop to stop the playing:
try {
Thread.sleep(NOTE_DURATION);
} catch (InterruptedException e) {
// XXX need to stop playing here, maybe return or break?
return;
}
由于 interrupt()
也可以在不同的时间出现,您需要测试中断状态并退出循环:
Since the interrupt()
can also come at a different time, you need to test for interrupt status and quit your loop:
if (!paused && !Thread.currentThread().isInterrupted()) {
...
此外,两个线程之间共享的所有变量要么需要同步
,要么标记为volatile
.paused
标志应该是 volatile
这里:
Also, all variables that are shared between two threads either need to be synchronized
or be marked volatile
. The paused
flag should probably be volatile
here:
volatile boolean paused = false
最后,为了后代,当你捕获 InterruptedException
时,它会清除线程的中断状态.通常最好立即在线程上设置中断标志,以便其他人可以对其进行测试:
Lastly, for posterity, when you catch InterruptedException
, it clears the interrupt status of the thread. It is usually good practice to immediately set the interrupt flag on the thread so others can test for it:
try {
Thread.sleep(NOTE_DURATION);
} catch (InterruptedException e) {
// re-establish the interrupt condition
Thread.currentThread.interrupt();
...
}
这篇关于即使我正在调用 thread.interrupt(),线程也不会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:即使我正在调用 thread.interrupt(),线程也不会中断
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01