JNI的用处 [英] Usefulness of JNI

查看:132
本文介绍了JNI的用处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成为一名Java开发人员已有几年了,并且听说过你可以用JNI做一些非常有用和强大的东西。我不知道我是不是需要使用它,或者它与我的关系不是很相关;但我根本没有碰过它。

I've been a java developer for a couple years and have heard that you can do some pretty useful and powerful things with JNI. I don't know if I just haven't needed to use it or if it isn't terribly relevant to me; but I have not had to touch it at all.

我想知道Java这方面的用处是什么。例子很棒。

I am wondering what the usefulness of this aspect of Java is. Examples would be great.

推荐答案

这非常有用。我可以看到使用JNI的两个主要原因(可能更多)。

It is very useful. I can see 2 primary reasons to use JNI (there are likely more).


  1. 性能。如果您有一段代码,其中Java Runtime无论出于何种原因都无法降低性能。您可以在本机代码中实现该功能,并从Java调用它。如果确实需要,这允许您手动调整实现。这可能是最不常见的原因,但Java通常表现得很好。

  1. Performance. If you have a piece of code in which the Java Runtime for whatever reason can't cut it performance wise. You can implement that function in native code and call it from Java. This allows you to hand tune the implementation if you really need to. This is likely the least common reason though, Java usually performs just fine.

访问OS特定API。这个是个大问题。我有一个案例,我需要用Java做一些事情,但也需要访问Java根本无法提供的东西。在我的例子中,它是UNIX域套接字。由于(正如您可以通过名称所知)它们是特定于UNIX的,因此没有标准的Java方法来使用它们。所以我在C中创建了一个类似于它们的包装器的类,并使用JNI访问它。中提琴,问题解决了。

Access to OS Specific APIs. This one is a biggie. I've had a case where I needed to do something in Java but also needed access to something that Java simply could not provide. In my case it was UNIX domain sockets. Since (as you can tell by the name) they are UNIX specific, there is no standard Java way to use them. So I made a class that acted like a wrapper around them in C and accessed it with JNI. viola, problem solved.

这篇关于JNI的用处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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