关于AttachCurrentThread和DetachCurrentThread的故事

来源:互联网 发布:淘宝口腔无影灯 编辑:程序博客网 时间:2024/05/24 06:50

关于AttachCurrentThread和DetachCurrentThread的故事


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

[java] view plain copy
 print?
  1. static JNIEnv *Adapter_GetEnv()  
  2. {  
  3.     int status;  
  4.     JNIEnv *envnow = NULL;  
  5.     status = (*g_JavaVM)->GetEnv(g_JavaVM,(void **) &envnow, JNI_VERSION_1_4);  
  6.     if(status < 0)  
  7.     {  
  8.         status = (*g_JavaVM)->AttachCurrentThread(g_JavaVM,&envnow, NULL);  
  9.         if(status < 0)  
  10.         {  
  11.             return NULL;  
  12.         }  
  13.         g_bAttatedT = TRUE;  
  14.     }  
  15.     return envnow;  
  16. }  
  17.   
  18. static void DetachCurrent()  
  19. {  
  20.     if(g_bAttatedT)  
  21.     {  
  22.         (*g_JavaVM)->DetachCurrentThread(g_JavaVM);  
  23.     }  
  24. }  

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

0
0 0
原创粉丝点击