简体   繁体   English

如何在JNI中将事件/信号从C发送到Java

[英]How to send events/signal from C to Java in JNI

Is there any mechanism to send asynchronous events/signals through JNI from C to Java or viceversa? 是否有通过JNI从C到Java或反之的异步事件/信号发送机制? I need to catch a signal/event from C to Java. 我需要捕获从C到Java的信号/事件。

It sounds a bit tricky, as JNI is supposed to be one-way only: Java code can invoke a native method, which of course may call back into Java code, but can't itself initiate the process. 听起来有些棘手,因为JNI应该是单向的:Java代码可以调用本机方法,当然可以调用本机方法,但是它本身不能启动该过程。

But it isn't impossible, I think the most straightforward solution is to have a native call that blocks until the signal is received. 但这并非不可能,我认为最直接的解决方案是阻塞本地调用,直到收到信号为止。

Another option would be to write a JVMTI agent, which can interfere with a running VM actively. 另一个选择是编写JVMTI代理,它可以主动干扰正在运行的VM。

But if we're talking about UNIX signals, you can handle then with the not public API (therefore not guaranteed in the future and Sun VM specific) but existing sun.misc.Signal class. 但是,如果我们谈论的是UNIX信号,则可以使用非公开的API(因此在将来无法保证,并且特定于Sun VM)但可以使用现有的sun.misc.Signal类进行处理。

You can call a Java method from C code. 您可以从C代码中调用Java方法。 See the JNI documentation. 请参阅JNI文档。 See also http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html . 另请参见http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html

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

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