0

I have executing method (for example JNI.calculate(listener))of JNI library. It should takes for example 30 seconds. I wanted but executing this method calculate(listener) again but first I need to stop last executing. This method has one parameter - listener, that has two methods:

public void canContinue(){
      return canContinue;
}

public void result(int number){
      //some code
}

The method canContinue calls asynchronous, for example every 3 seconds. I just dont want to wait, until next canContinue will be called. I wanted to immediatelly execute next execution of this method calculate(listener).

Do you have any recommendations? Probably I should use Handler, HandlerThread, Thread, AsyncTask or som sort of this classes, but how to figure it out?

user997777
  • 569
  • 1
  • 7
  • 19
  • did you find a solution? I'm trying to stop FFmpeg's execution (after calling it's main C function using Java native method) https://stackoverflow.com/questions/50664689/stop-cancel-execution-of-ffmpeg-command – user25 Jun 03 '18 at 12:22

2 Answers2

0

i didn't understand the question correctly but i think keeping a delay will do the trick, to keep delay write this

try {       
      Thread.sleep(1000); // time in milliseconds
    } catch (InterruptedException e) {
         e.printStackTrace();
      }
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • That is bad way how to do that. I could blocked the UI and the separating Thread. There must be a better way, I think. @Redman – user997777 Jul 01 '16 at 07:19
0

What if you call your function JNI.calculate(listener) from a Thread ? You can start that Thread which calls this function, and when you want an other and immediate execution, interrupt the Thread and start another (never restart a terminated thread!). Be careful with manipulated data: you should use a mutex to prevent it.

See that too (it's about thread interruption and gives some precisions):

https://stackoverflow.com/a/6378525/6523232

Community
  • 1
  • 1
N0un
  • 868
  • 8
  • 31
  • The method JNI.calculate(listener) is running on separate Thread, that is out of logic of the Android app. @N0un – user997777 Jul 01 '16 at 08:07
  • I understand. I found that too: http://stackoverflow.com/a/14814420/6523232 @user997777 – N0un Jul 01 '16 at 08:14