Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Netty使用反射将sun.nio.ch.SelectorImpl类中的成员替换为基于数组的集合?_Java_Reflection_Netty_Nio - Fatal编程技术网

Java 为什么Netty使用反射将sun.nio.ch.SelectorImpl类中的成员替换为基于数组的集合?

Java 为什么Netty使用反射将sun.nio.ch.SelectorImpl类中的成员替换为基于数组的集合?,java,reflection,netty,nio,Java,Reflection,Netty,Nio,在浏览Netty代码库时,我在NioEventLoop.java中遇到了下面的代码块 SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet(); Class<?> selectorImplClass = Class.forName("sun.nio.ch.SelectorImpl", false, PlatformDependent.getSystemClassLoad

在浏览Netty代码库时,我在NioEventLoop.java中遇到了下面的代码块

SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
Class<?> selectorImplClass =
                Class.forName("sun.nio.ch.SelectorImpl", false, PlatformDependent.getSystemClassLoader());

        // Ensure the current selector implementation is what we can instrument.
        if (!selectorImplClass.isAssignableFrom(selector.getClass())) {
            return selector;
        }

        Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
        Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");

        selectedKeysField.setAccessible(true);
        publicSelectedKeysField.setAccessible(true);

        selectedKeysField.set(selector, selectedKeySet);
        publicSelectedKeysField.set(selector, selectedKeySet);

        selectedKeys = selectedKeySet;
SelectedSelectionKeySet selectedKeySet=newselectedselectionkeyset();
类选择器示例类=
Class.forName(“sun.nio.ch.SelectorImpl”,false,平台相关的.getSystemClassLoader());
//确保当前选择器的实现是我们可以使用的工具。
如果(!selectorImplClass.isAssignableFrom(selector.getClass())){
返回选择器;
}
Field selectedKeysField=selectorImplClass.getDeclaredField(“selectedKeys”);
Field publicSelectedKeysField=selectorImplClass.getDeclaredField(“publicSelectedKeys”);
selectedKeysField.setAccessible(true);
publicSelectedKeysField.setAccessible(true);
selectedKeysField.set(选择器,selectedKeySet);
publicSelectedKeysField.set(选择器,selectedKeySet);
selectedKeys=selectedKeySet;
为什么Netty使用反射来更改java库类sun.nio.ch.SelectorImpl的成员


我看到了一个优点,它使用基于数组的集合,而不是使用java集合中的集合,我相信这会更快。还有其他具体原因吗?

主要是为了减少GC。Trustin在这里做了一些测试,发现这更有效。

这取决于SelectedSelectionKeySet类中是否有魔法,但这是一个非常糟糕的实践。