Effective Java 54 Use native methods judiciously

JNI使用指南
本文介绍了Java Native Interface (JNI) 的用途及历史应用场景,如访问平台特定设施、利用遗留代码库及提高性能关键部分的效率等。同时也讨论了使用本地方法可能带来的缺点,包括安全性问题、跨平台兼容性限制以及调试难度等。

Java Native Interface(JNI) allows Java applications to call native methods, which are special methods written in native programming languages such as C or C++.

Historical usages of native methods

  1. Access to platform-specific facilities such as registries and file locks.
  2. Access to libraries of legacy code.
  3. Access to native methods written for performance-critical part of application.

Disadvantage of native methods

  1. Native language are not safe (Item 39), native methods are no longer immune to memory corruption errors.
  2. Since native languages are platform dependent, application use them are far less possible.
  3. Difficult to debug.
  4. Fixed cost associated with going into and out of native code.
  5. It requires "glue code" that is difficult to read and tedious to write.

Summary

Think twice before using native methods. Rarely, if ever, use them for improved performance. If you must use native methods to access low-level resources or legacy libraries, use as little native code as possible and test it thoroughly. A single bug in the native code can corrupt your entire application.  

转载于:https://www.cnblogs.com/haokaibo/p/use-native-methods-judiciously.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值