0

I have a button listener witch include a thread sleep and another button listener.

Second button listener must interrupt this thread and I don t know how to do this:

My code:

button1.setOnClickListener (new View.OnClickListener() {

        @Override
        public void onClick(View v) {


..........


                button1.setEnabled(false);
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                button2.setEnabled(true);


                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2800);
                        } catch (InterruptedException e) {
                          // ???????
                        }

                        MainActivity.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                button1.setEnabled(true);
                                button1.setVisibility(View.VISIBLE);
                                button2.setEnabled(false);
                                button2.setVisibility(View.GONE);
                            }
                        });
                    }
                }).start();



 button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


..............


         //  Thread.interrupted(); -> does not work


                    }
                });

}   });

How can I make button2 listener to interrupt the thread?

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124

4 Answers4

1
   class TestInterruptingThread1 extends Thread{  
        public void run(){  
        try{  
        Thread.sleep(1000);  
        System.out.println("task");  
        }catch(InterruptedException e){  
        throw new RuntimeException("Thread interrupted..."+e);  
        }  

        }  

    b2 //
   {
    try{  
        t1.interrupt();  // t1 is the thread to be interrupted
        }catch(Exception e){System.out.println("Exception handled "+e);}  

        }
    } 
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
0

By calling the thread.interrupt() method. You also need to save your Thread instance in a field of your class.

yole
  • 92,896
  • 20
  • 260
  • 197
0
Thread t = new Thread() {...}
t.start();

and in the listener:

t.interrupt();

As to

//  Thread.interrupted(); -> does not work

it will work from within a class derived from Thread and will be the same as this.interrupted(); t.interrupted() will work outside of the class definition.

Also note that the method isInterrupted() does not clear the interruption flag, while interrupted() has the side-effect of clearing the flag.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

Instead of an anonymous implementation of the thread, make the thread a class variable. Suppose the name of the thread is btnOneThread

private Thread btnOneThread;

Initialize the thread where needed and start it.

btnOneThread = new Thread( ){...}
btnOneThread.start();

When the button2 is clicked call

if (btnOneThread != null) {
    btnOneThread.interrupt();
}
Randev
  • 36
  • 3