0

What's the way to stop a running method in Java?

I have a method called main in my MainActivity.java file and also I have two methods into it. They are blink() [which is a forever loop] and stop() . after a button(id=btn1) clicked I call the method blink() and I have an another button (id=btn2), on clicking on this button It will call the method stop(). Now I want that when ever i click on the button(btn2) the running method blink() will be stopped exactly on that moment.

What I actually need is -

public class MainActivity extends Activity {
    TextView tv1;
    Button btn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        @Override
        public void onClick(View _view) {
            _blink();
        }

        @Override
        public void onClick(View _view) {
            _stop();
        } 

        public void _blink() {
            // Here is my code which I want to perform as a forever loop
        }

        public void _stop() {
            //here I want the code which will stop the running method blink()
        }
    }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 2
    As an aside - these are *methods* that happen to have a void return type. There's no such thing as "a void". (And I don't think in this case the return type of the method is particularly relevant to the question.) – Jon Skeet Oct 19 '21 at 07:58
  • You're going to need to the code in blink to check for a condition and stop. I also think you need to start blink asynchronously so it doesn't consume the api. – matt Oct 19 '21 at 08:25
  • When you say “forever loop”, I hope you don’t mean an empty loop repeatedly checking (s as is bad practice, chewing up the CPU). Instead, you could create a `java.util.Timer` - see https://stackoverflow.com/questions/9053383/creating-a-repeating-timer-reminder-in-java/9053469 to control the blink-on and blink-off; To stop, simply call its `cancel` method. – racraman Oct 19 '21 at 08:37
  • How to impliment this cancel method – Malay Patra Oct 19 '21 at 09:23

2 Answers2

1

You can solve it with a flag variable itself right ? See below:

public class MainActivity extends Activity 
{
    
    TextView tv1;
    Button btn;
    private boolean isBlinking=false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    @Override
    public void onClick(View _view) {
      _blink();
}

    @Override
    public void onClick(View _view) {
      _stop();
} 

public void _blink(){
  this.isBlinking = true;
  while(this.isBlinking){...}
//here is my code which I want to perform as a forever loop


}

public void _stop(){
  this.isBlinking = false;

  //here I want the code which will stop the running method blink()

}

This works, if the activity runs as a thread (I'm not an Android guy :-) ). Otherwise, you have to run the _blink() in a thread and check the flag inside the loop of thread.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • 1
    This too needs a `volatile` on the `isBlinking` variable, otherwise it could be ignored when it's changed from another thread. – Joachim Sauer Oct 19 '21 at 08:27
0
volatile bool condition = false;
public void _blink()
{
    if (condition == true) return;
}
public void _stop()
{
    condition = true;
}