0

I have created a thread in a class, The code is as follows

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();
}

I stop the thread in a method

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

The problem I am facing is, The for loop in the thread seems to be running even after the successful call of Interrupt()

Any help here would be greatly appreciated!

Updated Code

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 Answers1

0

Purpose of interrupt(): It just sets the interrupted flag to true. After interrupt() has been called the Thread.currentThread().isInterrupted() starts to return false. And that's all.

Another case is if interrupt() is called while the thread is blocked in an invocation of one of the methods that throw InterruptedException, then that method will return throwing the InterruptedException. And if thread's code just "eats" that exception, then the thread will still continue running.

So the correct approach should be to periodically check the interrupted flag. And if interrupted status is detected then just return ASAP. Another common option is not to use Thread.interrupt() at all, but some custom boolean instead.

See the below link for safe stop of thread:-

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

Bette Devine
  • 1,196
  • 1
  • 9
  • 23
  • Dear Bette Devine, I have done the required changes. But still the thread continue to run. Do I need to delegate the Interrupted exception in the catch block ? – Nithin K S May 02 '14 at 05:51
  • 1
    Can you add a condition inside a for loop for checking whether the thread is interrupted then break the loop? – Bette Devine May 02 '14 at 06:02
  • I did try that. The problem I am facing now is, in the DEBUG mode, everything works fine. But When I run the thread, It fails. – Nithin K S May 02 '14 at 06:04
  • Please refer to my question above (Code Updated) – Nithin K S May 02 '14 at 06:11
  • See this thread, if it can help you - I solved my problem by going through this post once:http://stackoverflow.com/a/13552114/2035885. – Bette Devine May 02 '14 at 06:50