1

why when i call thread.start(); after thread.interrupt(); it gives me exception Thread already started. and app stop on samsung device with android 4.1.2 and doesn't give me that exception and app work normal on lenovo device with android 7 while it is the same code ??? it is a code to make flashlight blinker when i start the blinker then stop it and try to start again .. it stop on a device and work well on another device.

`

//thread blinker
    final Thread blinker = new Thread() {

        public void run() {

            while (!this.isInterrupted()) {
                try {
                    if (myCamera == null) {
                        try {

                            myCamera = Camera.open();

                        } catch (RuntimeException e) {

                        }
                    }

                    final Parameters p = myCamera.getParameters();



                    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    myCamera.setParameters(p);
                    myCamera.startPreview();

                    Thread.sleep(150);

                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    myCamera.setParameters(p);
                    myCamera.stopPreview();

                    Thread.sleep(150);


                } catch (InterruptedException e) {
                    final Parameters p = myCamera.getParameters();
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    myCamera.setParameters(p);
                    myCamera.stopPreview();
                    Thread.currentThread().interrupt();

                }
            }
        }
    };

//on button
        final Button onButton = findViewById(id.on);
        onButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                blinker.start();

            }
        });

  //off button
        final Button offButton = findViewById(id.off);
        offButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                          blinker.interrupt();

            }
        });

` when i add blinker.join(); with .interrupt(); it also gives me the same exception ... i'm new to java and i hope my question is not repeated

ProgEng
  • 11
  • 2

1 Answers1

0

Just because you interrupted a thread; doesn't mean it's finished. It will take a little time for that thread to process the exception, unwind the stack and clean up after itself.

If you're expecting it to finish you should join it before attempting to start again.

Without joining, what you have is a race condition. One thread is sending the interrupt and then trying to start; the other is receiving the interrupt and then quitting; and which will happen first is not guaranteed on any device.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • thanks for your quick reply .. but can you explain to me why it passes with the higher android version device? what is the relation between the error and device or android version? – ProgEng Mar 02 '18 at 14:46
  • i added the code .. and thread.join() is not helping so far :( – ProgEng Mar 02 '18 at 15:14
  • You may wish to read https://stackoverflow.com/questions/18223931/how-can-a-dead-thread-be-restarted (in short; you're trying to restart a dead thread; don't.) – UKMonkey Mar 02 '18 at 15:20