Thread interrupt vs. JNI Function Call

前端 未结 1 406
孤独总比滥情好
孤独总比滥情好 2021-01-23 14:57

I\'m using the method from the accepted answer here to construct a gameloop thread.

Where to stop/destroy threads in Android Service class?

At the moment, my thr

相关标签:
1条回答
  • 2021-01-23 15:28

    No worries, the documentation for interrupt says:

    If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the 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 an InterruptedException.

    So your thread will only get the InterruptedException if you're in some type of blocking/sleeping/waiting state. If you're running, the thread will not get the exception until it enters one of those states.

    Your loop should be:

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

    Update:
    Additionally, the documentation states:

    If none of the previous conditions hold then this thread's interrupt status will be set.

    0 讨论(0)
提交回复
热议问题