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(),线程也不会中断
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01