SelectorProvider定义

本文解析了SelectorProvider的作用及其在创建各种通道如DatagramChannel、Pipe、Selector等中的应用。详细介绍了SelectorProvider的初始化过程及默认实现。
Channel接口定义:[url]http://donald-draper.iteye.com/blog/2369111[/url]
AbstractInterruptibleChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369238[/url]
SelectableChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369317[/url]
SelectionKey定义:[url]http://donald-draper.iteye.com/blog/2369499[/url]
在上面的几篇文章中,经常看到一个类SelectorProvider,一直不知道什么意思,再往下分析之前,我们先看一下SelectorProvider的定义,以便后面更好的理解通道和选择器。
package java.nio.channels.spi;

import java.io.IOException;
import java.net.ProtocolFamily;
import java.nio.channels.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.ServiceConfigurationError;
import sun.security.action.GetPropertyAction;


/**
* Service-provider class for selectors and selectable channels.
*SelectorProvider主要是为创建选择器和可选择通道而服务的。
* A selector provider is a concrete subclass of this class that has a
* zero-argument constructor and implements the abstract methods specified
* below. A given invocation of the Java virtual machine maintains a single
* system-wide default provider instance, which is returned by the {@link
* #provider() provider} method. The first invocation of that method will locate
* the default provider as specified below.
*SelectorProvider的子类有一个无参的构造器,并实现了一些特殊的抽象方法,如下:
通过#provider调用java虚拟机维护一个系统默认的SelectorProvider实例。
*
* methods of the {@link java.nio.channels.DatagramChannel#open
* DatagramChannel}, {@link java.nio.channels.Pipe#open Pipe}, {@link
* java.nio.channels.Selector#open Selector}, {@link
* java.nio.channels.ServerSocketChannel#open ServerSocketChannel}, and {@link
* java.nio.channels.SocketChannel#open SocketChannel} classes. It is also
* used by the {@link java.lang.System#inheritedChannel System.inheritedChannel()}
* method. A program may make use of a provider other than the default provider
* by instantiating that provider and then directly invoking the <tt>open</tt>
* methods defined in this class.
*
系统默认的SelectorProvider被用于DatagramChannel,Pipe,Selector,ServerSocketChannel,
SocketChannel,System.inheritedChannel()的open方法中,用于创建相应的通道和选择器。
* <p> All of the methods in this class are safe for use by multiple concurrent
* threads.

*所有的方法可以并发安全访问。
*
* @author Mark Reinhold
* @author JSR-51 Expert Group
* @since 1.4
*/

public abstract class SelectorProvider {

private static final Object lock = new Object();
private static SelectorProvider provider = null;

/**
* Initializes a new instance of this class.
*初始化SelectorProvider实例,如果不为null,检查运行时权限
* @throws SecurityException
* If a security manager has been installed and it denies
* {@link RuntimePermission}<tt>("selectorProvider")</tt>
*/
protected SelectorProvider() {
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new RuntimePermission("selectorProvider"));
}
//根据系统属性java.nio.channels.spi.SelectorProvider配置
//加载SelectorProvider实例
private static boolean loadProviderFromProperty() {
//获取系统属性java.nio.channels.spi.SelectorProvider配置
String cn = System.getProperty("java.nio.channels.spi.SelectorProvider");
if (cn == null)
return false;
try {
Class<?> c = Class.forName(cn, true,
ClassLoader.getSystemClassLoader());
//加载SelectorProvider实例
provider = (SelectorProvider)c.newInstance();
return true;
} catch (ClassNotFoundException x) {
throw new ServiceConfigurationError(null, x);
} catch (IllegalAccessException x) {
throw new ServiceConfigurationError(null, x);
} catch (InstantiationException x) {
throw new ServiceConfigurationError(null, x);
} catch (SecurityException x) {
throw new ServiceConfigurationError(null, x);
}
}
//获取系统加载路径下的所有SelectorProvider实现类的实现,以最后一个作为SelectorProvider
private static boolean loadProviderAsService() {

ServiceLoader<SelectorProvider> sl =
ServiceLoader.load(SelectorProvider.class,
ClassLoader.getSystemClassLoader());
Iterator<SelectorProvider> i = sl.iterator();
for (;;) {
try {
if (!i.hasNext())
return false;
provider = i.next();
return true;
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore the security exception, try the next provider
continue;
}
throw sce;
}
}
}

/**
* Returns the system-wide default selector provider for this invocation of
* the Java virtual machine.
*返回JVM默认的SelectorProvider
* The first invocation of this method locates the default provider
* object as follows:

*首先调用本地默认的SelectorProvider,过程如下:
* [list=1]
*
* <li> If the system property
* <tt>java.nio.channels.spi.SelectorProvider</tt> is defined then it is
* taken to be the fully-qualified name of a concrete provider class.
* The class is loaded and instantiated; if this process fails then an
* unspecified error is thrown.
</li>
* 如果java.nio.channels.spi.SelectorProvider系统属性被定义为一个具体的SelectorProvider
实现类的唯一类名,则此类将会被加载,实例化,如果加载实例化失败,返回一个错误。
* <li> If a provider class has been installed in a jar file that is
* visible to the system class loader, and that jar file contains a
* provider-configuration file named
* <tt>java.nio.channels.spi.SelectorProvider</tt> in the resource
* directory <tt>META-INF/services</tt>, then the first class name
* specified in that file is taken. The class is loaded and
* instantiated; if this process fails then an unspecified error is
* thrown.
</li>
*如果SelectorProvider的实现在Jar包中,且对系统类加载器可见,且Jar在资源文件META-INF/services
的目录下,提供了provider-configuration文件java.nio.channels.spi.SelectorProvider,则文件的
第一个class类将会被加载和实例化,如果加载实例化失败,返回一个错误。

* <li> Finally, if no provider has been specified by any of the above
* means then the system-default provider class is instantiated and the
* result is returned.
</li>
* 如果上面两步没有发现或实例化SelectorProvider成功,则系统默认的SelectorProvider类,将会实例化。
* [/list]
*
* Subsequent invocations of this method return the provider that was
* returned by the first invocation.

*
* @return The system-wide default selector provider
*/
public static SelectorProvider provider() {
synchronized (lock) {
if (provider != null)
return provider;
//在与当前线程相同访问控制权限的环境中,加载SelectorProvider实例
return AccessController.doPrivileged(
new PrivilegedAction<SelectorProvider>() {
public SelectorProvider run() {
if (loadProviderFromProperty())
//获取系统配置的SelectorProvider
return provider;
if (loadProviderAsService())
//获取类加载路径下的SelectorProvider
return provider;
//加载默认的SelectorProvider
provider = sun.nio.ch.DefaultSelectorProvider.create();
return provider;
}
});
}
}

/**
* Opens a datagram channel. </p>
*打开一个DatagramChannel
* @return The new channel
*/
public abstract DatagramChannel openDatagramChannel()
throws IOException;

/**
* Opens a datagram channel.
*根据协议,打开一个DatagramChannel
* @param family
* The protocol family
*
* @return A new datagram channel
*
* @throws UnsupportedOperationException
* If the specified protocol family is not supported
* @throws IOException
* If an I/O error occurs
*
* @since 1.7
*/
public abstract DatagramChannel openDatagramChannel(ProtocolFamily family)
throws IOException;

/**
* Opens a pipe. </p>
* 打开一个Pipe
* @return The new pipe
*/
public abstract Pipe openPipe()
throws IOException;

/**
* Opens a selector. </p>
*打开一个Selector
* @return The new selector
*/
public abstract AbstractSelector openSelector()
throws IOException;

/**
* Opens a server-socket channel. </p>
*打开一个ServerSocketChannel
* @return The new channel
*/
public abstract ServerSocketChannel openServerSocketChannel()
throws IOException;

/**
* Opens a socket channel. </p>
*打开一个SocketChannel
* @return The new channel
*/
public abstract SocketChannel openSocketChannel()
throws IOException;

/**
* Returns the channel inherited from the entity that created this
* Java virtual machine.
*返回继承虚拟机创建实例的通道
* On many operating systems a process, such as a Java virtual
* machine, can be started in a manner that allows the process to
* inherit a channel from the entity that created the process. The
* manner in which this is done is system dependent, as are the
* possible entities to which the channel may be connected. For example,
* on UNIX systems, the Internet services daemon (<i>inetd</i>) is used to
* start programs to service requests when a request arrives on an
* associated network port. In this example, the process that is started,
* inherits a channel representing a network socket.
*在许多操作系统中,一个进程允许从创建此进程的实体继承一个通道。这种方式依赖
系统实现,也许实体到已连接的通道。在UNIX系统中,当在网络端口中,如果一个请求到达,
后台网络服务将会启动程序处理请求。在这个例子中,进程被启动,继承的通道表示一个网络socket。
* <p> In cases where the inherited channel represents a network socket
* then the {@link java.nio.channels.Channel Channel} type returned
* by this method is determined as follows:
*此方法返回的具体通道的过程如下:
* [list]
*
* <li><p> If the inherited channel represents a stream-oriented connected
* socket then a {@link java.nio.channels.SocketChannel SocketChannel} is
* returned. The socket channel is, at least initially, in blocking
* mode, bound to a socket address, and connected to a peer.
*
</li>
*如果继承通道表示一个面向流的连接Socket,则SocketChannel将会被返回。SocketChannel
初始化为阻塞模式,绑定一个socket地址,连接一个peer
* <li> If the inherited channel represents a stream-oriented listening
* socket then a {@link java.nio.channels.ServerSocketChannel
* ServerSocketChannel} is returned. The server-socket channel is, at
* least initially, in blocking mode, and bound to a socket address.
*
</li>
*如果继承通道表示一个面向流的监听socket,则ServerSocketChannel将会被返回。
ServerSocketChannel初始化为阻塞模式,绑定一个socket地址。
* <li> If the inherited channel is a datagram-oriented socket
* then a {@link java.nio.channels.DatagramChannel DatagramChannel} is
* returned. The datagram channel is, at least initially, in blocking
* mode, and bound to a socket address.
*
</li>
*如果继承的通道是一个面向报文的Socket,则DatagramChannel将会被返回,
初始化为阻塞模式,绑定一个socket地址。
* [/list]
*
* In addition to the network-oriented channels described, this method
* may return other kinds of channels in the future.
*目前就这三种继承通道,将会可能种类更多。
* <p> The first invocation of this method creates the channel that is
* returned. Subsequent invocations of this method return the same
* channel.

*第一次调用,则创建一个通道,后续的调用将会返回同一个通道
* @return The inherited channel, if any, otherwise <tt>null</tt>.
*
* @throws IOException
* If an I/O error occurs
*
* @throws SecurityException
* If a security manager has been installed and it denies
* {@link RuntimePermission}<tt>("inheritedChannel")</tt>
*
* @since 1.5
*/
public Channel inheritedChannel() throws IOException {
return null;
}

}

[size=medium][b]总结:[/b][/size]
[color=blue]SelectorProvider就是为了创建DatagramChannel,Pipe,Selector,ServerSocketChannel,SocketChannel,System.inheritedChannel()而服务的,在相应的通道和选择器的open方法中调用系统默认的SelectorProvider相关的open*方法,创建相应的通道和选择器。SelectorProvider的provider方法主要是实例化SelectorProvider,过程为:判断java.nio.channels.spi.SelectorProvider系统属性是否被定义为一个具体的SelectorProvider实现类的唯一类名,是则加载此类,实例化,如果加载实例化失败,返回一个错误。如果无没有选择器提供者属性配置,则在SelectorProvider的实现且对系统类加载器可见Jar包中,的资源文件META-INF/services的目录下,提供了provider-configuration文件java.nio.channels.spi.SelectorProvider,则文件的第一个class类将会被加载和实例化,如果加载实例化失败,返回一个错误。上两步失败,则加载系统默认的选择器提供者。inheritedChannel方法主要是更具系统网络服务,更具具体的网络请求,创建不同的可继承实例,如果继承通道表示一个面向流的连接Socket,则SocketChannel将会被返回。SocketChannel初始化为阻塞模式,绑定一个socket地址,连接一个peer。如果继承通道表示一个面向流的监听socket,则ServerSocketChannel将会被返回。ServerSocketChannel初始化为阻塞模式,绑定一个socket地址。如果继承的通道是一个面向报文的Socket,则DatagramChannel将会被返回,初始化为阻塞模式,绑定一个socket地址。[/color]

下面我们再来看一下SelectorProvider的provider方法系统默认的SelectorProvider:
//SelectorProvider
public static SelectorProvider provider() {
synchronized (lock) {
if (provider != null)
return provider;
//在与当前线程相同访问控制权限的环境中,加载SelectorProvider实例
return AccessController.doPrivileged(
new PrivilegedAction<SelectorProvider>() {
public SelectorProvider run() {
if (loadProviderFromProperty())
//获取系统配置的SelectorProvider
return provider;
if (loadProviderAsService())
//获取类加载路径下的SelectorProvider
return provider;
//加载默认的SelectorProvider
provider = sun.nio.ch.DefaultSelectorProvider.create();
return provider;
}
});
}
}

来看默认的DefaultSelectorProvider
//DefaultSelectorProvider
package sun.nio.ch;

import java.nio.channels.spi.SelectorProvider;

// Referenced classes of package sun.nio.ch:
// WindowsSelectorProvider

public class DefaultSelectorProvider
{
private DefaultSelectorProvider()
{
}
public static SelectorProvider create()
{
//默认的WindowsSelectorProvider
return new WindowsSelectorProvider();
}
}

再来看WindowsSelectorProvider
//WindowsSelectorProvider
package sun.nio.ch;

import java.io.IOException;
import java.nio.channels.spi.AbstractSelector;

// Referenced classes of package sun.nio.ch:
// SelectorProviderImpl, WindowsSelectorImpl

public class WindowsSelectorProvider extends SelectorProviderImpl
{

public WindowsSelectorProvider()
{
}
public AbstractSelector openSelector()
throws IOException
{
//默认的选择器实现类
return new WindowsSelectorImpl(this);
}
}

再来看SelectorProviderImpl
//SelectorProviderImpl
package sun.nio.ch;

import java.io.IOException;
import java.net.ProtocolFamily;
import java.nio.channels.*;
import java.nio.channels.spi.AbstractSelector;
import java.nio.channels.spi.SelectorProvider;

// Referenced classes of package sun.nio.ch:
// DatagramChannelImpl, PipeImpl, ServerSocketChannelImpl, SocketChannelImpl

public abstract class SelectorProviderImpl extends SelectorProvider
{

public SelectorProviderImpl()
{
}
//打开一个报文通道
public DatagramChannel openDatagramChannel()
throws IOException
{
return new DatagramChannelImpl(this);
}
//根据协议,打开一个报文通道
public DatagramChannel openDatagramChannel(ProtocolFamily protocolfamily)
throws IOException
{
return new DatagramChannelImpl(this, protocolfamily);
}
//打开一个管道
public Pipe openPipe()
throws IOException
{
return new PipeImpl(this);
}
//打开一个选择器,待子类扩展
public abstract AbstractSelector openSelector()
throws IOException;
//打开一个监听socket通道
public ServerSocketChannel openServerSocketChannel()
throws IOException
{
return new ServerSocketChannelImpl(this);
}
//打开一个socket通道(连接)
public SocketChannel openSocketChannel()
throws IOException
{
return new SocketChannelImpl(this);
}
}

从上面可以看出,WindowsSelectorProvider为系统默认选择器提供者,默认选择器为WindowsSelectorImpl,SelectorProviderImpl为默认的通道提供者,各类通道和管道的默认实现为:DatagramChannelImpl,ServerSocketChannelImpl,SocketChannelImpl,
PipeImpl。具体的通道,管道和选择器的实现,我们在相关文章会再次讲解。
我们来简单SocketChannel的open方法:
//SocketChannel
public abstract class ServerSocketChannel
extends AbstractSelectableChannel
implements NetworkChannel
{
public static ServerSocketChannel open() throws IOException {
return SelectorProvider.provider().openServerSocketChannel();
}
}

有了上面的分析,这个open应该很好理解。其他通道,选择器打开操作的思路是相同的。
<think>好的,我现在需要回答用户关于Netty中NioEventLoopGroup构造方法的问题。首先,我得回忆一下Netty的相关知识,特别是NioEventLoopGroup的构造方法。 我记得NioEventLoopGroup是Netty中处理I/O事件和任务的线程池,属于Reactor线程模型的一部分。它的构造方法有几个不同的重载版本,可能涉及线程数量、线程工厂、选择器策略等参数。 首先,应该确认构造方法的不同参数组合。比如默认无参构造方法,可能使用默认的线程数,也就是CPU核心数的两倍。然后可能有指定线程数的构造方法,比如传入一个int类型的nThreads参数。接着,可能还有接受ThreadFactory参数的构造方法,允许用户自定义线程创建方式。此外,可能还有结合SelectorProvider和选择策略的构造方法,比如使用特定的Provider来创建Selector,以及不同的策略来选择任务队列等。 需要检查这些构造方法是否存在。例如,NioEventLoopGroup是否有接受Executor、SelectorProvider、SelectStrategyFactory等参数的构造方法。另外,可能还有使用默认参数的构造方法,比如不指定线程数时自动计算。 接下来,我需要确认每个构造方法的具体作用。比如,指定线程数可以控制事件循环的并发线程数量;ThreadFactory可以自定义线程的名称、优先级等属性;SelectorProvider可能用于不同的平台或测试场景;选择策略则影响事件循环的行为,比如如何处理任务队列。 另外,需要注意不同Netty版本的差异,但用户没有指定版本,这里应该以较新的稳定版为准。此外,可能需要提及这些构造方法如何影响Netty的性能和资源管理,比如线程数过多或过少的影响,或者使用自定义线程工厂的好处。 最后,要组织这些信息,分点列出各个构造方法,并简要说明每个参数的作用。确保使用正确的中文术语,避免技术术语错误,同时保持回答结构清晰,易于理解。</think>Netty中的`NioEventLoopGroup`是处理I/O事件的核心线程池,其构造方法主要有以下几种形式: --- ### **1. 无参构造** ```java public NioEventLoopGroup() ``` - **作用**:使用默认线程数(CPU核心数 × 2)和默认配置创建线程组。 - **底层逻辑**:自动检测可用处理器核心数,适用于一般场景。 --- ### **2. 指定线程数** ```java public NioEventLoopGroup(int nThreads) ``` - **参数**:`nThreads` 定义事件循环线程数量。 - **用途**:手动控制并发线程数,优化资源分配(如高并发场景可适当增加)。 --- ### **3. 自定义线程工厂** ```java public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) ``` - **参数**: - `nThreads`:线程数量。 - `threadFactory`:自定义线程创建逻辑(如命名、优先级等)。 - **典型场景**:统一线程命名(如监控工具识别)或设置守护线程。 --- ### **4. 指定SelectorProvider** ```java public NioEventLoopGroup(int nThreads, SelectorProvider selectorProvider) ``` - **参数**: - `nThreads`:线程数量。 - `selectorProvider`:用于创建`Selector`的底层实现。 - **用途**:跨平台兼容或测试时替换默认Selector(如MacOS的KQueue)。 --- ### **5. 完整参数构造** ```java public NioEventLoopGroup( int nThreads, ThreadFactory threadFactory, SelectorProvider selectorProvider, SelectStrategyFactory selectStrategyFactory ) ``` - **参数**: - `nThreads`:线程数。 - `threadFactory`:自定义线程工厂。 - `selectorProvider`:Selector实现。 - `selectStrategyFactory`:事件循环策略(控制任务执行优先级)。 - **高级控制**:深度优化事件循环行为(如平衡I/O与任务处理)。 --- ### **关键特性** - **资源管理**:默认线程数为`Runtime.getRuntime().availableProcessors() * 2`,平衡性能与开销。 - **Selector优化**:通过`SelectorProvider`实现不同操作系统的高效I/O模型(如Linux的Epoll)。 - **线程模型**:每个`NioEventLoop`绑定一个线程,处理多路复用I/O和执行异步任务。 --- **示例代码**: ```java // 创建含4个线程的NIO事件组,自定义线程名称 NioEventLoopGroup group = new NioEventLoopGroup(4, new DefaultThreadFactory("NettyWorker", true) ); ``` 通过合理选择构造方法,可优化Netty应用的性能和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值