📄 tij0194.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0193.html">Prev</a> | <a href="tij0195.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Raw
Native Interface (RNI)
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Compared
to J/Direct, RNI is a fairly complex interface to non-Java code, but it’s
much more powerful. RNI is closer to the JVM than J/Direct, and this lets you
write much more efficient code, manipulate Java objects in your native methods,
and in general gives you a much higher degree of integration with the JVM
internal operations.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">RNI
is conceptually similar to Sun’s JNI. Because of this, and because the
product is not yet completed, I’ll just point out the major differences.
For further information, please refer to Microsoft’s documentation.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are several notable differences between JNI and RNI. Below is the C header file
generated by
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>msjavah</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the Microsoft equivalent of Sun’s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>javah</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
applied to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ShowMsgBox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
Java class file used previously for the JNI example.
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">/* DO NOT EDIT -
automatically generated by msjavah */</font>
#include <<font color="#0000ff">native</font>.h>
#pragma warning(disable:4510)
#pragma warning(disable:4512)
#pragma warning(disable:4610)
struct Classjava_lang_String;
#define Hjava_lang_String Classjava_lang_String
<font color="#009900">/* Header for class ShowMsgBox */</font>
#ifndef _Included_ShowMsgBox
#define _Included_ShowMsgBox
#define HShowMsgBox ClassShowMsgBox
typedef struct ClassShowMsgBox {
#include <pshpack4.h>
<font color="#0000ff">long</font> MSReserved;
#include <poppack.h>
} ClassShowMsgBox;
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) <font color="#0000ff">void</font> __cdecl
ShowMsgBox_ShowMessage (struct HShowMsgBox *,
struct Hjava_lang_String *);
#ifdef __cplusplus
}
#endif
#endif <font color="#009900">/* _Included_ShowMsgBox */</font>
#pragma warning(<font color="#0000ff">default</font>:4510)
#pragma warning(<font color="#0000ff">default</font>:4512)
#pragma warning(<font color="#0000ff">default</font>:4610) </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Apart
from being less readable, there are more technical issues disguised in the
code, which we’ll examine.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
RNI, the native method programmer knows the binary layout of the objects. This
allows you to directly access the information you want; you don’t need to
get a field or method identifier as in JNI. But since not all virtual machines
necessarily use the same binary layout for their objects, the native method
above is guaranteed to run only under the Microsoft JVM.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
JNI, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>JNIEnv</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
argument gives access to a large number of functions to interact with the JVM.
In RNI, the functions for controlling JVM operations are directly available.
Some of them, like the one for handling exceptions, are similar to their JNI
counterparts, but most of the RNI functions have different names and purposes
from those in JNI.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
of the most remarkable differences between JNI and RNI is the garbage
collection model. In JNI, the GC basically follows the same rules during native
method execution that it follows for the Java code execution. In RNI, the
programmer is responsible for starting and stopping the Garbage Collector
during native method activity. By default, the GC is disabled upon entering the
native method; doing so, the programmer can assume that the objects being used
will not be garbage collected during that time. But if the native method,
let’s say, is going to take a long time, the programmer is free to enable
the GC, calling the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GCEnable( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
RNI function.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
is also something similar to the global handles features – something the
programmer can use to be sure that specific objects will not be garbage
collected when the CG is enabled. The concept is similar but the name is
different: in RNI these guys are called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GCFrame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.</FONT><a name="_Toc408018833"></a><P></DIV>
<A NAME="Heading606"></A><H3 ALIGN=LEFT>
RNI
Summary
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
fact that RNI is tightly integrated with the Microsoft JVM is both its strength
and its weakness. RNI is more complex than JNI, but it also gives you a high
degree of control of the internal activities of the JVM, including garbage
collection. Also, it is clearly designed for speed, adopting compromises and
techniques that C programmers are familiar with. But it’s not suitable
for JVMs other than Microsoft’s.
</FONT><a name="_Toc408018834"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0193.html">Prev</a> | <a href="tij0195.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -