我可以多次调用JNI_CreateJavaVM吗?

时间:2010-02-01 18:21:21

标签: c++ multithreading java-native-interface

我试图启动两个称为“DispFrontEnd”函数的线程 第一个线程结束OK,第二个线程无法启动jvm .. ??

tks

#include "jni.h"
#include <process.h>
#include "Stdafx.h"
//DISPATCH Thread Check
bool DispatchThreadCreated = FALSE;


if (DispatchThreadCreated == FALSE)
{
   HANDLE hDispThread;

   hDispThread = (HANDLE)_beginthread(DispFrontEnd,0,(void *)dispatchInputs);

  if ((long)hDispThread == -1)
  {
   log.LogError("Thread DispFrontEnd Returned********BG ", (long)hDispThread);
   log.LogError("errno", errno);
   log.LogError("_doserrno", _doserrno);

  }
  else
  {
   logloc->LogMethod("Dispatch Thread CREATED");
   DispatchThreadCreated= TRUE;
   //Espera que a thread termine
   WaitForSingleObject( hDispThread, INFINITE );
   DispatchThreadCreated= FALSE;   // 01_02_2010
   logloc->LogMethod("Dispatch Thread ENDED");

  }
}

if (DispatchThreadCreated == FALSE)
{
   HANDLE hDispThread3;

   logloc->LogMethod("3 : Dispatch Thread CREATED");

   hDispThread3 = (HANDLE)_beginthread(DispFrontEnd,0,(void *)dispatchInputs);

  if ((long)hDispThread3 == -1)
  {
   log.LogError("3 : Thread DispFrontEnd Returned********BG ", (long)hDispThread3);
   log.LogError("errno", errno);
   log.LogError("_doserrno", _doserrno);

  }
  else
  {
   logloc->LogMethod("3 : Dispatch Thread CREATED");
   DispatchThreadCreated= TRUE;
   //Espera que a thread termine
   WaitForSingleObject( hDispThread3, INFINITE );
   DispatchThreadCreated= FALSE;   // 01_02_2010
   logloc->LogMethod("3 : Dispatch Thread ENDED");

  }
}




void DispFrontEnd(void * indArr)
{

 JNIEnv *env;
 JavaVM *jvm;
 env = create_vm(&jvm);  // return null on second call ???


}

JNIEnv* create_vm(JavaVM ** jvm) {


    CString str;    
    JNIEnv *env; 
    JavaVMInitArgs vm_args;
    JavaVMOption options;

    options.optionString = "-Djava.class.path=C:\\dispatch\\lib\\Run.jar;C:\\dispatch\\classes"; //Path to the java source code

    vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
    vm_args.nOptions = 1;
vm_args.options = &options;
    vm_args.ignoreUnrecognized = 0;

    int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);

    if(ret < 0)
    {
    env = NULL; 
    str.Format("ERROR! create JVM (%d)",ret);   // show this on second call!! ?
    logloc->LogMethod( str );

    }
    else
    {
            str.Format("JVM %x created Success!",env->GetVersion());
            logloc->LogMethod( str );

    }
    return env;
 }

1 个答案:

答案 0 :(得分:1)

你真的必须启动许多JVM吗?你能用吗? jint AttachCurrentThread(JavaVM * vm,JNIEnv ** p_env,void * thr_args);
相反?

我唯一知道的是本机线程无法同时连接两个不同的JVM。