1. <tfoot id='gf2yT'></tfoot>
      2. <legend id='gf2yT'><style id='gf2yT'><dir id='gf2yT'><q id='gf2yT'></q></dir></style></legend>
        <i id='gf2yT'><tr id='gf2yT'><dt id='gf2yT'><q id='gf2yT'><span id='gf2yT'><b id='gf2yT'><form id='gf2yT'><ins id='gf2yT'></ins><ul id='gf2yT'></ul><sub id='gf2yT'></sub></form><legend id='gf2yT'></legend><bdo id='gf2yT'><pre id='gf2yT'><center id='gf2yT'></center></pre></bdo></b><th id='gf2yT'></th></span></q></dt></tr></i><div id='gf2yT'><tfoot id='gf2yT'></tfoot><dl id='gf2yT'><fieldset id='gf2yT'></fieldset></dl></div>
        • <bdo id='gf2yT'></bdo><ul id='gf2yT'></ul>
      3. <small id='gf2yT'></small><noframes id='gf2yT'>

        插入USB热点后简单的Java程序慢100倍

        Simple Java program 100 times slower after plugging in USB hotspot(插入USB热点后简单的Java程序慢100倍)

        <small id='WcxJU'></small><noframes id='WcxJU'>

          <tbody id='WcxJU'></tbody>
      4. <legend id='WcxJU'><style id='WcxJU'><dir id='WcxJU'><q id='WcxJU'></q></dir></style></legend>

        • <i id='WcxJU'><tr id='WcxJU'><dt id='WcxJU'><q id='WcxJU'><span id='WcxJU'><b id='WcxJU'><form id='WcxJU'><ins id='WcxJU'></ins><ul id='WcxJU'></ul><sub id='WcxJU'></sub></form><legend id='WcxJU'></legend><bdo id='WcxJU'><pre id='WcxJU'><center id='WcxJU'></center></pre></bdo></b><th id='WcxJU'></th></span></q></dt></tr></i><div id='WcxJU'><tfoot id='WcxJU'></tfoot><dl id='WcxJU'><fieldset id='WcxJU'></fieldset></dl></div>
          • <bdo id='WcxJU'></bdo><ul id='WcxJU'></ul>
            <tfoot id='WcxJU'></tfoot>

                • 本文介绍了插入USB热点后简单的Java程序慢100倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下 Java 程序:

                  I have following Java program:

                  class Main {
                      public static void main(String[] args) throws java.io.IOException {
                          long start = System.nanoTime();
                          java.io.File.createTempFile("java_test", ".txt").delete();
                          System.out.println((System.nanoTime() - start ) / 1e9);
                      }
                  }
                  

                  通常,执行大约需要 63 毫秒:

                  Normally, it takes bout 63 miliseconds to execute:

                  $ java Main
                  0.06308555
                  

                  但是,一旦我将 Android 手机连接为 USB 热点,需要的时间会明显延长.取决于机器从 3 到 40 秒:

                  But, once I connect Android phone as USB hotspot, it takes significantly longer. Depending on the machine anywhere from 3 to 40 seconds:

                  $ java Main
                  4.263285528
                  

                  奇怪的是,这里实际上并没有通过网络传输任何东西——插入的网络适配器应该无关紧要.

                  The strange thing is that nothing here is actually transferred over the network - plugged network adapters shouldn't matter.

                  我做了一个回溯,看起来大部分时间都花在 NetworkInterface.getAll 方法上:

                  I did a backtrace and it looks like majority of time is spent in NetworkInterface.getAll method:

                  "main" #1 prio=5 os_prio=0 tid=0x00000000023ae000 nid=0x142c runnable [0x000000000268d000]
                     java.lang.Thread.State: RUNNABLE
                          at java.net.NetworkInterface.getAll(Native Method)
                          at java.net.NetworkInterface.getNetworkInterfaces(Unknown Source)
                          at sun.security.provider.SeedGenerator.addNetworkAdapterInfo(Unknown Source)
                          at sun.security.provider.SeedGenerator.access$000(Unknown Source)
                          at sun.security.provider.SeedGenerator$1.run(Unknown Source)
                          at sun.security.provider.SeedGenerator$1.run(Unknown Source)
                          at java.security.AccessController.doPrivileged(Native Method)
                          at sun.security.provider.SeedGenerator.getSystemEntropy(Unknown Source)
                          at sun.security.provider.SecureRandom$SeederHolder.<clinit>(Unknown Source)
                          at sun.security.provider.SecureRandom.engineNextBytes(Unknown Source)
                          - locked <0x000000076afa2820> (a sun.security.provider.SecureRandom)
                          at java.security.SecureRandom.nextBytes(Unknown Source)
                          - locked <0x000000076af6bdc8> (a java.security.SecureRandom)
                          at java.security.SecureRandom.next(Unknown Source)
                          at java.util.Random.nextLong(Unknown Source)
                          at java.io.File$TempDirectory.generateFile(Unknown Source)
                          at java.io.File.createTempFile(Unknown Source)
                          at java.io.File.createTempFile(Unknown Source)
                          at Main.main(Main.java:4)
                  

                  反过来,它似乎大部分时间都花在 GetIfTable Windows API 方法中:

                  which, in turn, seems to spend most of the time in GetIfTable Windows API method:

                  Child-SP          RetAddr           Call Site
                  00000000`0257ed78 000007fe`fd7210ba ntdll!NtDeviceIoControlFile+0xa
                  00000000`0257ed80 000007fe`fd721252 nsi+0x10ba
                  00000000`0257ee20 000007fe`fd7211f9 nsi!NsiEnumerateObjectsAllParametersEx+0x2e
                  00000000`0257ee60 000007fe`fd7217b0 nsi!NsiEnumerateObjectsAllParameters+0xc9
                  00000000`0257ef00 000007fe`f9c7928d nsi!NsiAllocateAndGetTable+0x184
                  00000000`0257efd0 00000000`6f8c5a01 IPHLPAPI!GetIfTable+0xa9
                  00000000`0257f090 00000000`6f8c6980 net!Java_java_net_NetworkInterface_getMTU0+0x1a1
                  00000000`0257f150 00000000`6f8c6e57 net!Java_java_net_NetworkInterface_isP2P0_XP+0x88
                  00000000`0257f270 00000000`6f8c6058 net!Java_java_net_NetworkInterface_getAll_XP+0x23
                  00000000`0257f2a0 00000000`02867f54 net!Java_java_net_NetworkInterface_getAll+0x2c
                  

                  GetIfTable 似乎是有问题的功能.我在以下示例程序中观察到同样的减速: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365943(v=vs.85).aspx 并带有以下代码段:

                  GetIfTable seems to be the problematic function. I'm observing the same slowdown both in example program from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365943(v=vs.85).aspx and with following snippet:

                  #include <iphlpapi.h>
                  #include <stdlib.h>
                  
                  int main() {
                      DWORD dwSize = sizeof(MIB_IFTABLE);
                      MIB_IFTABLE *pIfTable = malloc(dwSize);
                      GetIfTable(pIfTable, &dwSize, FALSE);
                      pIfTable = malloc(dwSize);
                      GetIfTable(pIfTable, &dwSize, FALSE);
                      return 0;
                  }
                  

                  如何修复或解决此问题?我可以自己创建临时文件,避免调用 NetworkInterface.getNetworkInterfaces,但 SecureRandom 用于 Java 标准库.有没有办法强制 SecureRandom 不使用 GetIfTable?

                  How do I fix or workaround this problem? I can create temporary files on my own and avoid calling NetworkInterface.getNetworkInterfaces but SecureRandom is used all over Java standard library. Is there a way to force SecureRandom not to use GetIfTable?

                  Java 版本:

                  > java -version
                  java version "1.8.0_101"
                  Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
                  Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
                  

                  Windows 版本:

                  Windows version:

                  OS Name:                   Microsoft Windows 7 Professional
                  OS Version:                6.1.7601 Service Pack 1 Build 7601
                  

                  有问题的网络适配器:

                  Name    [00000020] Remote NDIS based Internet Sharing Device
                  Adapter Type    Ethernet 802.3
                  Product Type    Remote NDIS based Internet Sharing Device
                  Installed   Yes
                  PNP Device ID   USBVID_0FCE&PID_71C4&MI_007&6BE3F3B&0&0000
                  Last Reset  8/14/2016 12:26 PM
                  Index   20
                  Service Name    usb_rndisx
                  IP Address  192.168.42.183, fe80::90ab:3786:4396:2870
                  IP Subnet   255.255.255.0, 64
                  Default IP Gateway  192.168.42.129
                  DHCP Enabled    Yes
                  DHCP Server 192.168.42.129
                  DHCP Lease Expires  8/14/2016 3:27 PM
                  DHCP Lease Obtained 8/14/2016 2:27 PM
                  MAC Address 02:18:61:77:7D:72
                  Driver  c:windowssystem32driversusb8023x.sys (6.1.7600.16385, 19.50 KB (19,968 bytes), 7/14/2009 2:09 AM)
                  

                  推荐答案

                  SecureRandom的默认实现 扫描网络接口作为系统熵的额外来源.为了避免这种情况,您需要注册一个自定义 java.security.Provider 包含 SecureRandomSpi.

                  Default implementation of SecureRandom scans network interfaces as an additional source of system entropy. In order to avoid this, you need to register a custom java.security.Provider that contains a different implementation of SecureRandomSpi.

                  幸运的是,JDK for Windows 已经有一个合适的 SecureRandomSpi 实现,它依赖于 Microsoft Crypto API:sun.security.mscapi.PRNG.虽然这是非公共 API,但该类存在于从 1.6 到 9 的所有版本的 OpenJDK 和 Oracle JDK 中,并且无论如何都可以使用回退.

                  Fortunately, JDK for Windows already has a suitable SecureRandomSpi implementation that relies on Microsoft Crypto API: sun.security.mscapi.PRNG. Though this is non-public API, the class exists in all versions of OpenJDK and Oracle JDK from 1.6 to 9, and the fallback is available anyway.

                  有两种方法可以将 MS Crypto PRNG 注册为默认的 SecureRandom 算法.

                  There are two ways to register MS Crypto PRNG as the default SecureRandom algorithm.

                  1.在应用程序的一开始就调用 WindowsSecureRandom.register().

                  1. From within the application by calling WindowsSecureRandom.register() at the very beginning.

                  import java.security.Provider;
                  import java.security.Security;
                  
                  public class WindowsSecureRandom extends Provider {
                      private static final String MSCAPI = "sun.security.mscapi.PRNG";
                  
                      private WindowsSecureRandom() {
                          super("WindowsSecureRandom Provider", 1.0, null);
                          putService(new Service(this, "SecureRandom", "Windows-PRNG", MSCAPI, null, null));
                      }
                  
                      public static void register() {
                          if (System.getProperty("os.name").contains("Windows")) {
                              try {
                                  Class.forName(MSCAPI);
                                  Security.insertProviderAt(new WindowsSecureRandom(), 1);
                              } catch (ClassNotFoundException e) {
                                  // Fallback to default implementation
                              }
                          }
                      }
                  }
                  

                  2.通过重新排序 %JAVA_HOME%jrelibsecurityjava.security 文件中的提供程序列表.

                  2. By reordering provider list in %JAVA_HOME%jrelibsecurityjava.security file.

                  security.provider.1=sun.security.mscapi.SunMSCAPI  <<<--- make it the first provider
                  security.provider.2=sun.security.provider.Sun
                  security.provider.3=sun.security.rsa.SunRsaSign
                  security.provider.4=sun.security.ec.SunEC
                  security.provider.5=com.sun.net.ssl.internal.ssl.Provider
                  ...
                  

                  我已验证,无论使用哪种解决方案,SeedGeneratorNetworkInterface 类都不再加载.

                  I've verified that with either solutions SeedGenerator and NetworkInterface classes are no longer loaded.

                  这篇关于插入USB热点后简单的Java程序慢100倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                        <i id='kouCi'><tr id='kouCi'><dt id='kouCi'><q id='kouCi'><span id='kouCi'><b id='kouCi'><form id='kouCi'><ins id='kouCi'></ins><ul id='kouCi'></ul><sub id='kouCi'></sub></form><legend id='kouCi'></legend><bdo id='kouCi'><pre id='kouCi'><center id='kouCi'></center></pre></bdo></b><th id='kouCi'></th></span></q></dt></tr></i><div id='kouCi'><tfoot id='kouCi'></tfoot><dl id='kouCi'><fieldset id='kouCi'></fieldset></dl></div>
                          <tfoot id='kouCi'></tfoot>
                            <tbody id='kouCi'></tbody>
                          <legend id='kouCi'><style id='kouCi'><dir id='kouCi'><q id='kouCi'></q></dir></style></legend>
                        • <small id='kouCi'></small><noframes id='kouCi'>

                          • <bdo id='kouCi'></bdo><ul id='kouCi'></ul>