<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## Chapter 9. General Programming(通用程序設計) ### Item 66: Use native methods judiciously(明智地使用本地方法) The Java Native Interface (JNI) allows Java programs to call native methods, which are methods written in native programming languages such as C or C++. Historically, native methods have had three main uses. They provide access to platform-specific facilities such as registries. They provide access to existing libraries of native code, including legacy libraries that provide access to legacy data. Finally, native methods are used to write performance-critical parts of applications in native languages for improved performance. Java 本地接口(JNI)允許 Java 程序調用本地方法,這些方法是用 C 或 C++ 等本地編程語言編寫的。從歷史上看,本地方法主要有三種用途。它們提供對特定于平臺的設施(如注冊中心)的訪問。它們提供對現有本地代碼庫的訪問,包括提供對遺留數據訪問。最后,本地方法可以通過本地語言編寫應用程序中注重性能的部分,以提高性能。 It is legitimate to use native methods to access platform-specific facilities, but it is seldom necessary: as the Java platform matured, it provided access to many features previously found only in host platforms. For example, the process API, added in Java 9, provides access to OS processes. It is also legitimate to use native methods to use native libraries when no equivalent libraries are available in Java. 使用本地方法訪問特定于平臺的機制是合法的,但是很少有必要:隨著 Java 平臺的成熟,它提供了對許多以前只能在宿主平臺中上找到的特性。例如,Java 9 中添加的流 API 提供了對 OS 流程的訪問。在 Java 中沒有等效庫時,使用本地方法來使用本地庫也是合法的。 **It is rarely advisable to use native methods for improved performance.** In early releases (prior to Java 3), it was often necessary, but JVMs have gotten much faster since then. For most tasks, it is now possible to obtain comparable performance in Java. For example, when java.math was added in release 1.1, BigInteger relied on a then-fast multiprecision arithmetic library written in C. In Java 3, BigInteger was reimplemented in Java, and carefully tuned to the point where it ran faster than the original native implementation. **為了提高性能,很少建議使用本地方法。** 在早期版本(Java 3 之前),這通常是必要的,但是從那時起 JVM 變得更快了。對于大多數任務,現在可以在 Java 中獲得類似的性能。例如,在版本 1.1 中添加了 `java.math`,BigInteger 是在一個用 C 編寫的快速多精度運算庫的基礎上實現的。在當時,為了獲得足夠的性能這樣做是必要的。在 Java 3 中,BigInteger 則完全用 Java 重寫了,并且進行了性能調優,新的版本比原來的版本更快。 A sad coda to this story is that BigInteger has changed little since then, with the exception of faster multiplication for large numbers in Java 8. In that time, work continued apace on native libraries, notably GNU Multiple Precision arithmetic library (GMP). Java programmers in need of truly high-performance multiprecision arithmetic are now justified in using GMP via native methods [Blum14]. 這個故事的一個可悲的結尾是,除了在 Java 8 中對大數進行更快的乘法運算之外,BigInteger 此后幾乎沒有發生什么變化。在此期間,對本地庫的工作繼續快速進行,尤其是 GNU 多精度算術庫(GMP)。需要真正高性能多精度算法的 Java 程序員現在可以通過本地方法使用 GMP [Blum14]。 The use of native methods has serious disadvantages. Because native languages are not safe (Item 50), applications using native methods are no longer immune to memory corruption errors. Because native languages are more platform-dependent than Java, programs using native methods are less portable. They are also harder to debug. If you aren’t careful, native methods can decrease performance because the garbage collector can’t automate, or even track, native memory usage (Item 8), and there is a cost associated with going into and out of native code. Finally, native methods require “glue code” that is difficult to read and tedious to write. 使用本地方法有嚴重的缺點。由于本地語言不安全([Item-50](/Chapter-8/Chapter-8-Item-50-Make-defensive-copies-when-needed.md)),使用本地方法的應用程序不再能免受內存毀壞錯誤的影響。由于本地語言比 Java 更依賴于平臺,因此使用本地方法的程序的可移植性較差。它們也更難調試。如果不小心,本地方法可能會降低性能,因為垃圾收集器無法自動跟蹤本地內存使用情況([Item-8](/Chapter-2/Chapter-2-Item-8-Avoid-finalizers-and-cleaners.md)),而且進出本地代碼會產生相關的成本。最后,本地方法需要「粘合代碼」,這很難閱讀,而且編寫起來很乏味。 In summary, think twice before using native methods. It is rare that you need to use them for improved performance. If you must use native methods to access low-level resources or native libraries, use as little native code as possible and test it thoroughly. A single bug in the native code can corrupt your entire application. 總之,在使用本地方法之前要三思。一般很少需要使用它們來提高性能。如果必須使用本地方法來訪問底層資源或本地庫,請盡可能少地使用本地代碼,并對其進行徹底的測試。本地代碼中的一個錯誤就可以破壞整個應用程序。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-9/Chapter-9-Introduction.md)** - **Previous Item(上一條目):[Item 65: Prefer interfaces to reflection(接口優于反射)](/Chapter-9/Chapter-9-Item-65-Prefer-interfaces-to-reflection.md)** - **Next Item(下一條目):[Item 67: Optimize judiciously(明智地進行優化)](/Chapter-9/Chapter-9-Item-67-Optimize-judiciously.md)**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看