松山湖仿做网站,爱站小工具,网站如何诊断,口碑营销的案例1.Selector和Channel关系
Selector一般称为选择器#xff0c;也叫多路复用器#xff0c;NIO的核心组件#xff0c;用于检查一个或多个Channel的状态是否处于可读、可写的状态。
2.可选择通道
#xff08;1#xff09;不是所有的channel都能被selector复用#xff0c;…1.Selector和Channel关系
Selector一般称为选择器也叫多路复用器NIO的核心组件用于检查一个或多个Channel的状态是否处于可读、可写的状态。
2.可选择通道
1不是所有的channel都能被selector复用就是要看channel是否继承SelectableChannel如果继承了就是可以复用。例如FileChannel是不能被selector复用的。 2一个通道可以被注册到多个选择骑上但每个选择器只能被注册一次。在注册的时候需要指定通道那些操作是选择器感兴趣的。
3.Channel注册到Selector
Channel.register(Selector sel, int ops) 方法将一个通道注册到一个选择器上。第一个参数指定通道要注册的选择器。第二个参数指定选择器需要查询的通道操作。可以供选择器查询的通道操作包括如下四种可以通过位或操作符表示多种操作类型感兴趣 1可读 SelectionKey.OP_READ 2可写SelectionKey.OP_WRITE 3连接SelectionKey.OP_CONNECT 4接收SelectionKey.OP_ACCEPT
3.1 选择键SelectionKey
1Channel注册后一旦通道处于某种就绪的状态就可以被选择器查询到。 2Selector不断查询Channel中操作的就绪状态并挑选感兴趣的操作就绪状态并放入选择键集合。 3一个选择键包含了特定通道域特定选择器之间的注册关系。 NIO就是根据对应的选择键进行不同的业务逻辑处理。
3.2 Selector的使用方法 (1) Channel必须处于非阻塞模式下否则会抛出IllegalBlockingModeException (2)一个通道不一定支持所有的四种操作比如ServerSocketChannel支持Accept操作SocketChannel则不支持。可通过validOps()获取支持的操作集合。 Selector的创建 // 创建SelectorSelector sl Selector.open();// 创建通道ServerSocketChannel serverSocketChannel ServerSocketChannel.open();// 非阻塞serverSocketChannel.configureBlocking(false);// 绑定连接serverSocketChannel.bind(new InetSocketAddress(9999));//将通道注册到选择器上serverSocketChannel.register(sl, SelectionKey.OP_ACCEPT);System.out.println(serverSocketChannel.validOps());2.轮询查询就绪操作
//查询已经就绪通道操作SetSelectionKey selectionKeys sl.selectedKeys(); // 遍历集合IteratorSelectionKey iterator selectionKeys.iterator();while (iterator.hasNext()) {SelectionKey next iterator.next();// 判断key的就绪状态操作if (next.isAcceptable()) {} else if (next.isConnectable()) {} else if (next.isReadable()) {} else {}iterator.remove();}4 实验案例
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import org.junit.jupiter.api.Test;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;/*** Des* Date*/
public class Demo2 {Test// 客户端代码public void client() throws IOException {// 获取通道绑定主机和端口号SocketChannel socketChannel SocketChannel.open(new InetSocketAddress(127.0.0.1, 8080));// 切换为非阻塞模式socketChannel.configureBlocking(false);// 创建bufferByteBuffer byteBuffer ByteBuffer.allocate(1024);// 写入bufferbyteBuffer.put(new Date().toString().getBytes());// 读写模式转换byteBuffer.flip();// channel写入数据socketChannel.write(byteBuffer);// 清空bufferbyteBuffer.clear();}Test// 客户端代码public void Serverdemo() throws IOException {// 获取通道绑定主机和端口号ServerSocketChannel serverSocketChannel ServerSocketChannel.open();// 非阻塞serverSocketChannel.configureBlocking(false);// 创建bufferByteBuffer byteBuffer ByteBuffer.allocate(1024);// 绑定连接serverSocketChannel.bind(new InetSocketAddress(8080));// 获取selector选择器Selector sl Selector.open();// 通道注册到选择器并进行监听serverSocketChannel.register(sl, SelectionKey.OP_ACCEPT );// 选择器进行轮询while (sl.select() 0) {SetSelectionKey selectionKeys sl.selectedKeys();IteratorSelectionKey iterator selectionKeys.iterator();while (iterator.hasNext()) {SelectionKey next iterator.next();// 判断何种类型操作if (next.isAcceptable()) {// 获取连接SocketChannel accpet serverSocketChannel.accept();//切换非阻塞模式accpet.configureBlocking(false);// 注册accpet.register(sl, SelectionKey.OP_READ);} else if (next.isReadable()) {SocketChannel socketChannel (SocketChannel) next.channel();// 读取数据int length 0;while ((length socketChannel.read(byteBuffer)) 0) {byteBuffer.flip();System.out.println(receiver new String(byteBuffer.array(), 0, length));}}}iterator.remove();}}} 文章转载自: http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.trwkz.cn.gov.cn.trwkz.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.lsjgh.cn.gov.cn.lsjgh.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.hjssh.cn.gov.cn.hjssh.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.fygbq.cn.gov.cn.fygbq.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.bqfpm.cn.gov.cn.bqfpm.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.mumgou.com.gov.cn.mumgou.com