查了一下 JDK 的 source code(我用的是 jdk-1_5_0-src-scsl),在 jni.cpp 里看到这样一段,hehe~~_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) { // At the moment it's only possible to have one Java VM, // since some of the runtime state is in global variables. // We cannot use our mutex locks here, since they only work on // Threads. We do an atomic compare and exchange to ensure only // one thread can call this method at a time if (Atomic::add(1, &num_vm_created) > 1) { return JNI_ERR; } if (Atomic::xchg(1, &vm_created) == 1) { Atomic::dec(&num_vm_created); return JNI_ERR; } assert(vm_created == 1, "vm_created is true during the creation"); ……
maquan('ma:kju) 意思是说只能创建一个jvm了...
看来就是只能创建一个 JVM 了,既然 Sun 的 JDK 就是这么做的~~我也写了个 C 程序试过了,没什么新发现 :(
2次jvm.dll一样没用。
也许Unix/Linux下可以。但是确实没有必要这么做。
我是在两个线程分别调用"JNI_CreateJavaVM"的,而且调用的时间也不同。
Win32上java.exe就是一个虚拟机,是一个进程。里面可能有多个ClassLoader加载多个App。
// At the moment it's only possible to have one Java VM,
// since some of the runtime state is in global variables. // We cannot use our mutex locks here, since they only work on
// Threads. We do an atomic compare and exchange to ensure only
// one thread can call this method at a time
if (Atomic::add(1, &num_vm_created) > 1) {
return JNI_ERR;
}
if (Atomic::xchg(1, &vm_created) == 1) {
Atomic::dec(&num_vm_created);
return JNI_ERR;
} assert(vm_created == 1, "vm_created is true during the creation");
……
意思是说只能创建一个jvm了...