简体   繁体   English

在同一过程中执行Java和C#?

[英]Executing Java and C# within the same process?

I want to execute a Java VM and a C# application within the same process making it possible to share the same memory region along with the same memory for the same shared libraries. 我想在同一过程中执行Java VM和C#应用程序,从而可以为相同的共享库共享相同的内存区域和相同的内存。

Does anyone know a way to do that? 有人知道这样做的方法吗?

I would be able to run Java from C# or C# from Java or even using a C application to start both within the same process. 我将能够从C#或Java中的C#运行Java,甚至可以使用C应用程序在同一进程中同时启动两者。

[Update] [更新]

The http://jni4net.com/ project claims to provide the Feature: "Intraprocess - it means that both VMs are in same process. Any call uses same thread and same stack for both environments. It's relatively fast." http://jni4net.com/项目声称提供了以下功能:“进程内-这意味着两个VM处于同一进程中。任何调用在两个环境中都使用相同的线程和相同的堆栈。这相对较快。”

This is exactly what I try to do myself in order to use a simple off-heap memory address and use it to both intra-process VMs. 我正是尝试自己这样做,以便使用一个简单的堆外内存地址并将其用于两个进程内VM。

C# is .NET which is compiled to an Intermediary Language (IL), it is possible that Java has a processor for the .NET IL, but it would be need to be included as part of the Java project when built. C#是被编译为中间语言(IL)的.NET,Java可能具有用于.NET IL的处理器,但在构建时将其包含在Java项目中。

You would not be able to run two separate processes that share the same execution thread and memory. 您将无法运行共享相同执行线程和内存的两个单独的进程。

Bottom line is I don't think this is a good idea. 最重要的是,我认为这不是一个好主意。

From my personal experience, all the currently available projects which claimed to support both are having various defects and support issues on libraries and even some language features, not to even mention the run-time performance. 从我的个人经验来看,所有声称支持两者的当前可用项目在库甚至某些语言功能上都存在各种缺陷和支持问题,更不用说运行时性能了。 If you don't prefer a simple socket solution or a memory mapped file stuff and declare them to be non-standard and with pitfalls, then I could only tell you the frameworks you trying to use are with even more pitfalls. 如果您不喜欢简单的套接字解​​决方案或内存映射的文件内容,并且将它们声明为非标准且存在陷阱,那么我只能告诉您您尝试使用的框架存在更多陷阱。

Plus, the idea of sharing memory is a bit puzzling me. 另外,共享内存的想法让我有些困惑。 What exact data piece are you trying to share? 您要共享什么确切的数据? Bear in mind that the C# and Java memory structure are not mutually compatible, you either storing some stuff with 3rd party standard eg audio/video/pictures etc; 请记住,C#和Java内存结构不相互兼容,您要么存储一些第三方标准的东西,例如音频/视频/图片等; or you need to write your own serialization layer which essentially disable you from being able to operate on the same object from both sides. 或者您需要编写自己的序列化层,这实际上使您无法从两侧对同一对象进行操作。 If you are already writing your own serialization layer, what blocks you from using a socket solution which seems best supported from both side to me? 如果您已经在编写自己的序列化层,那么什么使您无法使用似乎双方都支持最好的套接字解​​决方案呢?

If there is any performance concern, I could say that since you are using Java + C# solution, the performance difference between MMF, socket or shared memory is insignificant compared to whatever performance your application can possibly achieve. 如果有任何性能方面的问题,我可以说由于您使用的是Java + C#解决方案,因此与应用程序可以实现的任何性能相比,MMF,套接字或共享内存之间的性能差异都微不足道。 If your application really requires such a performance, you'd better do it in C++ or even ANSI C. 如果您的应用程序确实需要这样的性能,则最好使用C ++甚至是ANSIC。

So I totally agree with all the comments, that the stuff you are trying to achieve seems not in the right direction. 因此,我完全同意所有评论,即您试图实现的目标似乎方向不正确。

The solution is to embed Java and Mono within the same process. 解决方案是将Java和Mono嵌入同一进程中。 There are resources available for doing so within a C/C++ application but it is not limited to C/C++. 在C / C ++应用程序中有可用的资源,但不仅限于C / C ++。

By embedding Java and Mono (C#) together within the same process, all the desired properties can be guaranteed. 通过在同一过程中将Java和Mono(C#)嵌入在一起,可以保证所有所需的属性。 There is no duplication in the shared memory, memory manipulation is instantaneously and the changes appear in the correct order. 共享内存中没有重复项,内存操作是即时的,并且更改以正确的顺序显示。

One should take care to pin the threads according to the memory domain in multiple processor setups as always but thats about it. 人们应该像往常一样小心地根据多个处理器设置中的内存域来固定线程,仅此而已。

It is even possible to share the same processor by pinning the threads of C# and Java to the same processor and to profit from second level and even third level caches. 甚至可以通过将C#和Java线程固定到同一处理器来共享同一处理器,并从第二级甚至第三级缓存中获利。

Another important benefit is both VMs profit from sharing the same local shared library instance resulting in code running in either of the VMs interacting with the very same library instance instead of using two distinct versions. 另一个重要的好处是,两个VM都受益于共享相同的本地共享库实例,从而导致在两个VM中运行的代码与完全相同的库实例进行交互,而不是使用两个不同的版本。

Helpful information: 有用的信息:

There are ways to host a JVM in a .NET process, or to host a .NET CLR in a Java process, but they are ugly and require familiarity with JNI (Java Native Interface) and/or the CLR's COM-based hosting API. 有几种方法可以在.NET进程中托管JVM,也可以在Java进程中托管.NET CLR,但是它们很丑陋,并且需要熟悉JNI(Java本机接口)和/或CLR基于COM的托管API。

There are fortunately interoperability tools that encapsulate these mechanisms and allow Java and .NET to run in the same process and freely interoperate. 幸运的是,有一些互操作性工具可以封装这些机制,并允许Java和.NET在同一进程中运行并可以自由互操作。 One such tool is JNBridgePro from JNBridge. 一种这样的工具是来自JNBridge的JNBridgePro。 Please see www.jnbridge.com for more information, and particularly look for "shared memory." 请访问www.jnbridge.com了解更多信息,尤其是寻找“共享内存”。

Disclaimer: I am with JNBridge. 免责声明:我在JNBridge。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM