通过Delphi中的JNI创建的JVM创建多个线程 [英] JVM created via JNI in Delphi creates multiple threads

查看:174
本文介绍了通过Delphi中的JNI创建的JVM创建多个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JNI和delphi有问题.

I have a problem with the JNI and delphi.

每当我使用以下代码在delphi中创建JVM时:

Whenever I create a JVM in delphi with this code:

  FillChar(Options, SizeOf(Options), #0);
  Options[0].optionString := '-Djava.class.path=' + 'blabla.jar';
  VM_args.version := JNI_VERSION_1_2;
  VM_args.options := @Options;
  VM_args.nOptions := 1;

  JVM := TJavaVM.Create(JNI_VERSION_1_6, getJvmPath());
  Errorcode := JVM.LoadVM(VM_args);

我尝试使用此代码关闭JVM,但是只有1个线程被关闭:

I try to close the JVM with this code, but only 1 thread get closed:

  JNIEnv.Free;
  JNIEnv := nil;

  JVM.JavaVM^.DetachCurrentThread(JVM.JavaVM);
  JVM.JavaVM^.DestroyJavaVM(JVM.JavaVM);
  UnloadJVM;

  JVM.Free;
  JVM := nil;

我的应用程序创建了4-6个线程,这些线程是在此处的最后一行代码中创建的.

My application creates 4-6 threads, the threads are created at the last line of code here.

我尝试使用DetachCurrentThread()DestroyJavaVM()UnloadJVM解决此问题,但是最多只有1个线程消失.

I tried to use DetachCurrentThread(), DestroyJavaVM() and UnloadJVM to resolve this issue, but then only 1 thread disappears max.

是否有摆脱这些线程的方法?

Is there a way to get rid of those threads?

提前谢谢!

推荐答案

根据Java JNI文档,

According to Java JNI documentation, Chapter 5 - The Invocation API:

卸载VM

JNI_DestroyJavaVM()函数卸载Java VM.

Unloading the VM

The JNI_DestroyJavaVM() function unloads a Java VM.

从JDK/JRE 1.1开始,只有主线程可以通过调用DestroyJavaVM卸载VM.从JDK/JRE 1.2开始,该限制已删除,并且任何线程都可以调用DestroyJavaVM来卸载VM.

As of JDK/JRE 1.1, only the main thread could unload the VM, by calling DestroyJavaVM. As of JDK/JRE 1.2, the restriction was removed, and any thread may call DestroyJavaVM to unload the VM.

VM等待直到当前线程是唯一的非守护程序用户线程,然后才实际卸载.

用户线程包括Java线程和附加的本机线程.

User threads include both Java threads and attached native threads.

之所以存在此限制,是因为Java线程或附加的本机线程可能正在保存系统资源,例如锁,窗口等. VM无法自动释放这些资源.

This restriction exists because a Java thread or attached native thread may be holding system resources, such as locks, windows, and so on. The VM cannot automatically free these resources.

通过将当前线程限制为卸载VM时唯一正在运行的线程,释放由任意线程持有的系统资源的负担就由程序员承担.

By restricting the current thread to be the only running thread when the VM is unloaded, the burden of releasing system resources held by arbitrary threads is on the programmer.

VM不会自动完成线程的线程化,直到线程终止,它才会完全卸载.

The VM will not automatically finalize your threads, and it will not completely unload until they terminate.

您有责任完成自己的线程,但是我猜您所引用的额外线程是由JVM自身在加载时创建的.

It is your responsability that your own threads get finished, but I guess that the extra threads that you are referring are created by the JVM itself when loaded.

还要注意,JNI文档有些含糊:

Also note that the JNI documentation is somewhat ambiguous:

销毁JavaVM

卸载Java VM并回收其资源.

DestroyJavaVM

Unloads a Java VM and reclaims its resources.

在JDK/JRE 1.1中,对DestroyJavaVM的支持不完整.从JDK/JRE 1.1开始,只有主线程可以调用DestroyJavaVM.

The support for DestroyJavaVM was not complete in JDK/JRE 1.1. As of JDK/JRE 1.1 Only the main thread may call DestroyJavaVM.

从JDK/JRE 1.2开始,任何线程(无论是否连接)都可以调用此函数.如果连接了当前线程,则VM会等待直到当前线程是唯一的非守护程序用户级Java线程.如果未附加当前线程,则VM会附加当前线程,然后等待直到当前线程是唯一的非守护程序用户级线程.

Since JDK/JRE 1.2, any thread, whether attached or not, can call this function. If the current thread is attached, the VM waits until the current thread is the only non-daemon user-level Java thread. If the current thread is not attached, the VM attaches the current thread and then waits until the current thread is the only non-daemon user-level thread.

不过,JDK/JRE仍然不支持VM卸载.

因此看来,直到程序退出,JVM才完全卸载.

So it seems that anyway the JVM is not completely unloaded until the program exits.

这篇关于通过Delphi中的JNI创建的JVM创建多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆