即使我正在调用thread.interrupt(),线程也不会中断 [英] Thread not interrupting even though I'm calling thread.interrupt()

查看:264
本文介绍了即使我正在调用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()) {
   ...

此外,两个线程之间共享的所有变量都需要 synchronized 或标记为 volatile 暂停标志应该是 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

最后,对于后代,当你catch 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(),线程也不会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆