关于AttachCurrentThread和DetachCurrentThread的故事

当在一个线程里面调用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

你可能感兴趣的:(java,c++,开发语言)