views:

173

answers:

1

Hello

I´m Trying to launch two threads witch calls "DispFrontEnd" function First thread ended OK, second failed to start 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;
 }
A: 

Do you really have to start many JVM ? Could you use
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args);
instead ?

The only thing I know is a native thread cannot attach two different JVM at the same time.

Bob Yoplait
yes the first thread ended and maybe JVM was not yet destroyed before the second thread started...
Reversed
Thanks for your help AttachCurrentThread() worked!
Reversed