现在的位置: 首页 > 综合 > 正文

关于AttachCurrentThread和DetachCurrentThread的故事

2012年08月10日 ⁄ 综合 ⁄ 共 729字 ⁄ 字号 评论关闭

当在一个线程里面调用AttachCurrentThread后,如果不需要用的时候一定要DetachCurrentThread,否则线程无法正常退出。

static JNIEnv *Adapter_GetEnv()
{
	int status;
	JNIEnv *envnow = NULL;
	status = (*g_JavaVM)->GetEnv(g_JavaVM,(void **) &envnow, JNI_VERSION_1_4);
	if(status < 0)
	{
		status = (*g_JavaVM)->AttachCurrentThread(g_JavaVM,&envnow, NULL);
		if(status < 0)
		{
			return NULL;
		}
		g_bAttatedT = TRUE;
	}
	return envnow;
}

static void DetachCurrent()
{
	if(g_bAttatedT)
	{
		(*g_JavaVM)->DetachCurrentThread(g_JavaVM);
	}
}

07-24 15:02:23.874: DEBUG/dalvikvm(4932): threadid=9: thread exiting, not yet detached (count=0)
07-24 15:02:23.874: DEBUG/dalvikvm(4932): threadid=9: thread exiting, not yet detached (count=1)
07-24 15:02:23.874: ERROR/dalvikvm(4932): threadid=9: native thread exited without detaching
07-24 15:02:23.874: ERROR/dalvikvm(4932): VM aborting

【上篇】
【下篇】

抱歉!评论已关闭.