Thread interrupt vs. JNI Function Call(线程中断与 JNI 函数调用)
问题描述
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 thread basically gets the time, makes a single Native Function call that updates the game logic, then sleeps by the adjusted passed time.
What I'm curious of, since I'm still not very comfortable with Threads, is how fast a Thread is killed with interrupt()? If it is in the middle of the code running in the Native Function, will it stop in the middle of it, or will it safely complete?
Thanks ahead of time, Jeremiah
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.
这篇关于线程中断与 JNI 函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:线程中断与 JNI 函数调用


基础教程推荐
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01