线程中断与JNI函数调用 [英] Thread interrupt vs. JNI Function Call

查看:523
本文介绍了线程中断与JNI函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的方法从接受的答案在这里构建一个gameloop线程。

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

<一个href=\"http://stackoverflow.com/questions/680180/where-to-stop-destroy-threads-in-android-service-class\">Where停止/摧毁Android的服务类线程?

目前,我基本上线程获取时间,使一个单一的本地函数调用,更新游戏逻辑,然后睡在调整时间的流逝。

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.

我很好奇什么的,因为我仍然不是很舒服的主题,是主题是如何快速杀死与中断()?如果是在本机函数的code运行的中间,它会停在它的中间,或者将它安全地完成?

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:

如果此线程被阻塞在等待(的调用),等待(长),或等待(长,INT)Object类的方法,或在连接(),连接(长),连接(长,INT),睡眠(长),或睡眠(长,INT),这个类,方法则其中断状态将被清除,它还将收到一个InterruptedException。

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.

所以,你的线程将只能获得 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.

您循环应该是:

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

更新:结果
此外,该文件规定:

Update:
Additionally, the documentation states:

如果没有previous条件成立,那么该线程的中断状态将被设置。

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

这篇关于线程中断与JNI函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆