I have a difficult problem calling my own function using JNI from a thread.
A native function is legacy code that performs a task requiring computation. Since I do not want to freeze the rest of the program, the calculation should be done in the background thread. EventBus is used to send the calculation result to the main program.
Basically it should be pretty simple, something like this:
public class CalculationEngine { private CalculationEngine(){} public static void calculateInBackground(final Parameters parameters) { new Thread(new Runnable() { public void run() {
Now, the calculateNormally method, which blocks the main program, works fine, but the calculateInBackground method, which simply creates a background thread to do the same, causes various crashes in its own code when it is called sequentially, I call it again again only after the previous thread completed and returned the result. Note that native code is marked synchronized to ensure that only one instance can work at a time.
My question is, how can the native code actually behave differently depending on whether it is called from the main thread or from some other thread? It, like its own code, retained a “state” and did not stop when it was called from a thread other than the main thread. Is there a way to “clean” or “flush” a stream after it is completed? There should be something in JNI and Threads that I just don't know.
Thanks for any tips!
source share