Interrupting a stream versus a JNI function call

I am using the method from the accepted answer here to build a gameloop stream.

Where to stop / destroy threads in Android class?

Right now, my thread is basically getting time, making one Native Function call that updates the game logic, and then sleeping on the adjusted elapsed time.

What I'm wondering, since I still don't really like Threads, how quickly does a Thread get killed by interrupt ()? If it's in the middle of the code being executed in the Native Function, will it stop in the middle, or will it terminate safely?

Thanks in advance Jeremiah

+1


source to share


1 answer


Don't worry, the documentation for interrupt

says
:

If this thread is blocked when calling the methods wait (), wait (long), or wait (long, int) of the Object class or join (), join (long), join (long, int), sleep (long), or sleep (long, int), methods of this class, then its interrupt status will be cleared and it will receive InterruptedException.

This way, your thread will only receive InterruptedException

if you are in block / hibernate / standby state. If you are running, the thread will not receive an exception until it enters one of these states.

Your loop should be:



while(!Thread.currentThread().isInterrupted()) // <- something of the sort here
{
    try{
        // do work
    } catch (InterruptedException e){
        // clean up
    }
}

      

Update:
Also, the documentation states:

If none of the previous conditions are met, this thread interrupt status will be set.

+3


source







All Articles