无法杀死Android中的线程

时间:2014-05-02 05:18:35

标签: android multithreading

我在类中创建了一个线程,代码如下

private void startThread() {
    if (t != null) {
        t.interrupt();
    }
    isFlashOn = true;

    t = new Thread() {
        public void run() {
            try {
                try {
                    SurfaceView surfaceView = (SurfaceView) activity
                            .findViewById(R.id.surfaceViewCam);
                    SurfaceHolder surfaceHolder = surfaceView.getHolder();
                    // surfaceHolder.addCallback(this);
                    camera.setPreviewDisplay(surfaceHolder);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                camera.startPreview();

                for (int i = seekBarManager.preferenceManager
                        .get_duration(); i > 0 && !this.isInterrupted(); i--) {
                    isFlashOn = true;
                    setBlinkToggle();
                    sleep(seekBarManager.preferenceManager.get_delay());
                    if(isFlashOn==false){
                        break;
                    }
                }

                if (camera != null) {
                    camera.stopPreview();
                    camera.release();
                    camera = null;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };

    t.start();
}

我在方法中停止线程

private void stopThread() {
    if (t != null) {
        t.interrupt();
        //t.
        isFlashOn = false;
    }
}

我面临的问题是,即使在成功调用Interrupt()

之后,线程中的for循环似乎仍在运行

非常感谢任何帮助!

更新代码

t = new Thread() {
            public void run() {
                Boolean isStop = false;
                try {
                    try {
                        SurfaceView surfaceView = (SurfaceView) activity
                                .findViewById(R.id.surfaceViewCam);
                        SurfaceHolder surfaceHolder = surfaceView.getHolder();
                        // surfaceHolder.addCallback(this);
                        camera.setPreviewDisplay(surfaceHolder);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    camera.startPreview();

                    for (int i = seekBarManager.preferenceManager
                            .get_duration(); i > 0 && !isStop; i--) {
                        isFlashOn = true;
                        setBlinkToggle();
                        sleep(seekBarManager.preferenceManager.get_delay());
                    }

                    if (camera != null) {
                        camera.stopPreview();
                        camera.release();
                        camera = null;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    isStop=true;
                    //notify();
                    return;
                }

            }
        };

1 个答案:

答案 0 :(得分:0)

中断的目的():  它只是将中断标志设置为true。在调用interrupt()之后,Thread.currentThread()。isInterrupted()开始返回false。这就是全部。

另一种情况是,如果在调用其中一个抛出InterruptedException的方法时阻塞线程,则调用interrupt(),然后该方法将返回抛出InterruptedException。如果线程的代码只是"吃掉"那个例外,那么线程仍将继续运行。

所以正确的方法应该是定期检查中断的标志。如果检测到中断状态,则尽快返回。另一个常见的选择是根本不使用Thread.interrupt(),而是使用一些自定义布尔值。

请参阅以下链接以安全停止线程: -

http://www.java2s.com/Code/Java/Threads/Thesafewaytostopathread.htm

相关问题