线程中断替代? [英] Thread interrupt alternatives?

查看:147
本文介绍了线程中断替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SOS模式构建手电筒应用程序。有3个按钮(开,关和SOS)。应用程序在正常的开启和关闭模式下工作,但在SOS模式下不工作。(SOS模式不关闭)

Building a Flashlight App with SOS mode. Has 3 buttons( On, Off and SOS). App works in normal On and Off mode but not in SOS mode.(SOS mode doesnt switch off)

//this method gets called when Off button is pressed
    private void turnOffFlash() {
            if (FlashOn) {
                if (myCamera == null || myParameters == null) {
                    return;
                }
                myParameters = myCamera.getParameters();
                myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                myCamera.setParameters(myParameters);
                myCamera.stopPreview();
                try {
                    if (SOSon)
                        Flashthread.interrupt();
                    SOSon = false;
                } catch (Exception ex) {
                    throw ex;
                }
                FlashOn = false;
                number_of_press=0;
            }
        }

Flashthread

void onSOSPress() {
        if (number_of_press == 1) {
            try {
                SOSon = true;
                if (!Flashthread.isInterrupted()) {
                    if (SOSon) {
                        Flashthread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                for (int i = 0; i < System.currentTimeMillis(); i++) {
                                    if (FlashOn) {
                                        myParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                                        myCamera.setParameters(myParameters);
                                        FlashOn = false;
                                    } else {
                                        TurnOnFlash();
                                    }
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }
                        });
                        Flashthread.start();
                    }
                } else
                    Flashthread.resume();
            } catch (Exception ex) {
                throw ex;
            }
        }
    }

turnOffFlash 方法,因为我读到中断方法并没有真正中断/杀死线程,我可以用什么代替 Thread.Interrupt(); 这样按 Off 按钮会停止 SOS 模式?
我尝试了 stop() destroy(),但两者都崩溃了。

In the turnOffFlash method,since I read that the Interrupt method doesn't really "interrupt"/kill the thread,what can I use instead of Thread.Interrupt(); so that pressing the Off button stops the SOS mode? I tried the stop() and destroy() but both crashed the app.

推荐答案

你应该使用的是 Handler ,如评论中所建议的,但如果你想要坚持使用这个系统,使用一个标志告诉你的线程停止:

What you should use is a Handler as suggested in the comments, but if you want to stick with this system, use a flag to tell your thread to stop:

boolean shouldStop = false;

...
while (!shouldStop){
  if(FlashOn){
    ...//do SOS stuff
  }
}

...
public void endSOS(){
  shouldStop = true;
}

这篇关于线程中断替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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