当前位置: 首页 > news >正文

苏州网站建设功能广州seo服务公司

苏州网站建设功能,广州seo服务公司,wordpress 线条不显示不出来,哪些网站可以找到做海报的素材深入理解BIO与NIO BIO BIO 为 Blocked-IO(阻塞 IO),在 JDK1.4 之前建立网络连接时,只能使用 BIO 使用 BIO 时,服务端会对客户端的每个请求都建立一个线程进行处理,客户端向服务端发送请求后,…

深入理解BIO与NIO

BIO

BIO 为 Blocked-IO(阻塞 IO),在 JDK1.4 之前建立网络连接时,只能使用 BIO

使用 BIO 时,服务端会对客户端的每个请求都建立一个线程进行处理,客户端向服务端发送请求后,先咨询服务端是否有线程响应,如果没有就会等待或者被拒绝

BIO 基本使用代码:

服务端:

public class TCPServer {public static void main(String[] args) throws Exception {// 1.创建ServerSocket对象System.out.println("服务端 启动....");System.out.println("初始化端口 7777 ");ServerSocket ss = new ServerSocket(7777); //端口号while (true) {// 2.监听客户端Socket s = ss.accept(); //阻塞// 3.从连接中取出输入流来接收消息InputStream is = s.getInputStream(); //阻塞byte[] b = new byte[10];is.read(b);String clientIP = s.getInetAddress().getHostAddress();System.out.println(clientIP + "说:" + new String(b).trim());// 4.从连接中取出输出流并回话OutputStream os = s.getOutputStream();os.write("服务端回复".getBytes());// 5.关闭s.close();}}
}

客户端:

public class TCPClient {public static void main(String[] args) throws Exception {while (true) {// 1.创建Socket对象Socket s = new Socket("127.0.0.1", 7777);// 2.从连接中取出输出流并发消息OutputStream os = s.getOutputStream();System.out.println("请输入:");Scanner sc = new Scanner(System.in);String msg = sc.nextLine();os.write(msg.getBytes());// 3.从连接中取出输入流并接收回话InputStream is = s.getInputStream(); //阻塞byte[] b = new byte[20];is.read(b);System.out.println("客户端发送消息:" + new String(b).trim());// 4.关闭s.close();}}
}

BIO 缺点:

  • Server 端会为客户端的每一个连接请求都创建一个新的线程进行处理,如果客户端连接请求数量太多,则会创建大量线程

NIO

从 JDK1.4 开始,Java 提供了一系列改进的输入/输出的新特性,被统称为 NIO(即 New IO),NIO 弥补了 BIO 的不足,在服务端不需要为客户端大量的请求而建立大量的处理线程,只需要用很少的线程就可以处理很多客户端请求

NIO 和 BIO 有着相同的目的和作用,但是它们的实现方式完全不同;

  • BIO 以流的方式处理数据,而 NIO 以块的方式处理数据,块 IO 的效率比流 IO 高很多。
  • NIO 是非阻塞式的,这一点跟 BIO 也很不相同,使用它可以提供非阻塞式的高伸缩性网络。

NIO 有三大核心部分

  • Channel通道
  • Buffer缓冲区
  • Selector选择器

使用 NIO 时,数据是基于 ChannelBuffer 进行操作的,数据从 Channel 被读取到 Buffer 或者相反,Selector 用于监听多个 Channel 通道的事件(连接事件、读写事件),通过 Selector 就可以实现单个线程来监听多个客户端通道

NIO 中的 Channel 用来建立到目标的一个连接,在 BIO 中流是单向的,例如 FileInputStream 只能进行读取操作,而在 NIO 中 Channel 是双向的,既可以读也可以写

NIO 工作流程图如下:Server 端通过单线程来监听多个客户端 Channel 通道中的事件并进行处理

在这里插入图片描述

NIO 使用示例

服务端:

public class NIOServer {public static void main(String[] args) throws Exception {// 1. 开启一个ServerSocketChannel通道ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();// 2. 开启一个Selector选择器Selector selector = Selector.open();// 3. 绑定端口号8888System.out.println("服务端 启动....");System.out.println("初始化端口 8888 ");serverSocketChannel.bind(new InetSocketAddress(8888));// 4. 配置非阻塞方式serverSocketChannel.configureBlocking(false);// 5. Selector选择器注册ServerSocketChannel通道,绑定连接操作serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);// 6. 循环执行:监听连接事件及读取数据操作while (true) {// 6.1 监控客户端连接:selecto.select()方法返回的是客户端的通道数,如果为0,则说明没有客户端连接。if (selector.select(2000) == 0) {System.out.println("服务端等待客户端连接中~");continue;}// 6.2 得到SelectionKey,判断通道里的事件Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();// 遍历所有SelectionKeywhile (keyIterator.hasNext()) {SelectionKey key = keyIterator.next();// 客户端连接请求事件if (key.isAcceptable()) {System.out.println("服务端处理客户端连接事件:OP_ACCEPT");SocketChannel socketChannel = serverSocketChannel.accept();socketChannel.configureBlocking(false);// 服务端建立与客户端之间的连接通道 SocketChannel,并且将该通道注册到 Selector 中,监听该通道的读事件socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));}// 读取客户端数据事件if (key.isReadable()) {// 数据在通道中,先拿到通道SocketChannel channel = (SocketChannel) key.channel();// 取到一个缓冲区,nio读写数据都是基于缓冲区。ByteBuffer buffer = (ByteBuffer) key.attachment();// 从通道中将客户端发来的数据读到缓冲区channel.read(buffer);System.out.println("客户端数据长度:" + buffer.array().length);System.out.println("客户端发来数据:" + new String(buffer.array()));}//  6.3 手动从集合中移除当前key,防止重复处理keyIterator.remove();}}}
}

客户端:

public class NIOClient {public static void main(String[] args) throws Exception {// 1. 得到一个网络通道SocketChannel channel = SocketChannel.open();// 2. 设置非阻塞方式channel.configureBlocking(false);// 3. 提供服务器端的IP地址和端口号InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8888);// 4. 连接服务器端,如果用connect()方法连接服务器不成功,则用finishConnect()方法进行连接if (!channel.connect(address)) {// 因为接需要花时间,所以用while一直去尝试连接。在连接服务端时还可以做别的事,体现非阻塞。while (!channel.finishConnect()) {// nio 作为非阻塞式的优势,如果服务器没有响应(不启动服务端),客户端不会阻塞,最后会报错,客户端尝试连接服务器连不上。System.out.println("客户端等待连接建立时,执行其他任务~");}}// 5. 得到一个缓冲区并存入数据String msg = "客户端发送消息:hello";ByteBuffer writeBuf = ByteBuffer.wrap(msg.getBytes());// 6. 发送数据channel.write(writeBuf);// 阻止客户端停止,否则服务端也会停止。System.in.read();}
}

AIO

JDK 7 引入了 Asynchronous IO,即 AIO,叫做异步不阻塞的 IO,也可以叫做 NIO2

在进行 IO 编程中,常用到两种模式:Reactor模式 和 Proactor 模式

  • NIO采用 Reactor 模式,当有事件触发时,服务器端得到通知,进行相应的处理
  • AIO采用 Proactor 模式,引入异步通道的概念,简化了程序编写,一个有效的请求才启动一个线程,它的特点是先由操作系统完成后,才通知服务端程序启动线程去处理,一般适用于连接数较多且连接时间较长的应用

文章转载自:
http://dinnconeuraxitis.ydfr.cn
http://dinncothoughtful.ydfr.cn
http://dinncoshowman.ydfr.cn
http://dinncomartin.ydfr.cn
http://dinncodendrophilous.ydfr.cn
http://dinncoabraham.ydfr.cn
http://dinncoinfimum.ydfr.cn
http://dinncoolap.ydfr.cn
http://dinncosomatoplasm.ydfr.cn
http://dinnconinette.ydfr.cn
http://dinncoscenograph.ydfr.cn
http://dinncomycelial.ydfr.cn
http://dinncoimparticipable.ydfr.cn
http://dinncoattentive.ydfr.cn
http://dinncochittamwood.ydfr.cn
http://dinncocapreomycin.ydfr.cn
http://dinncocommeasurable.ydfr.cn
http://dinncopredicament.ydfr.cn
http://dinncowartwort.ydfr.cn
http://dinncosimulation.ydfr.cn
http://dinncocountermine.ydfr.cn
http://dinncotailpipe.ydfr.cn
http://dinncobecquerel.ydfr.cn
http://dinncokhansamah.ydfr.cn
http://dinncohcg.ydfr.cn
http://dinncovaricolored.ydfr.cn
http://dinncoinquisition.ydfr.cn
http://dinncoaluminise.ydfr.cn
http://dinncoconclude.ydfr.cn
http://dinncoconspiracy.ydfr.cn
http://dinncofungi.ydfr.cn
http://dinncodisesteem.ydfr.cn
http://dinncobulbiform.ydfr.cn
http://dinncotahine.ydfr.cn
http://dinncogeneva.ydfr.cn
http://dinncomariposa.ydfr.cn
http://dinncoaweigh.ydfr.cn
http://dinncorole.ydfr.cn
http://dinncoenterology.ydfr.cn
http://dinncoglacis.ydfr.cn
http://dinncoinp.ydfr.cn
http://dinncojournalese.ydfr.cn
http://dinncoballplayer.ydfr.cn
http://dinncobandore.ydfr.cn
http://dinncocache.ydfr.cn
http://dinncoanaclasis.ydfr.cn
http://dinncoamphoric.ydfr.cn
http://dinncotakovite.ydfr.cn
http://dinncoripping.ydfr.cn
http://dinncosaxboard.ydfr.cn
http://dinncoinfrequency.ydfr.cn
http://dinncoimpossibly.ydfr.cn
http://dinncopoint.ydfr.cn
http://dinncostrenuous.ydfr.cn
http://dinncophotogravure.ydfr.cn
http://dinncocavitation.ydfr.cn
http://dinncobaas.ydfr.cn
http://dinncohorseflesh.ydfr.cn
http://dinncocordiality.ydfr.cn
http://dinncocafeteria.ydfr.cn
http://dinncoheadsail.ydfr.cn
http://dinncocolorant.ydfr.cn
http://dinncogilderoy.ydfr.cn
http://dinncomorale.ydfr.cn
http://dinncolatinity.ydfr.cn
http://dinncoaeromotor.ydfr.cn
http://dinncotremor.ydfr.cn
http://dinncophyle.ydfr.cn
http://dinncosorrowfully.ydfr.cn
http://dinncodoctorial.ydfr.cn
http://dinncogingham.ydfr.cn
http://dinncobepowder.ydfr.cn
http://dinncobursectomize.ydfr.cn
http://dinncointrogression.ydfr.cn
http://dinncolungfish.ydfr.cn
http://dinncofmn.ydfr.cn
http://dinncomenthene.ydfr.cn
http://dinncodaa.ydfr.cn
http://dinncoadopted.ydfr.cn
http://dinncoovercapitalization.ydfr.cn
http://dinncosavarin.ydfr.cn
http://dinncomughal.ydfr.cn
http://dinncodiaphragmatic.ydfr.cn
http://dinnconarcotine.ydfr.cn
http://dinncohiaa.ydfr.cn
http://dinncodolorous.ydfr.cn
http://dinncoarteriography.ydfr.cn
http://dinncomapai.ydfr.cn
http://dinncoamateurish.ydfr.cn
http://dinncowife.ydfr.cn
http://dinncomeasly.ydfr.cn
http://dinncononvocoid.ydfr.cn
http://dinncothreshold.ydfr.cn
http://dinncoautotomize.ydfr.cn
http://dinncomassecuite.ydfr.cn
http://dinncodisparagingly.ydfr.cn
http://dinncosubliterate.ydfr.cn
http://dinncouptight.ydfr.cn
http://dinncohybridist.ydfr.cn
http://dinncooutachieve.ydfr.cn
http://www.dinnco.com/news/138550.html

相关文章:

  • 做网站的技巧企业网站推广方案设计
  • 成都那家网站做的好吉林百度查关键词排名
  • 网站建设哪家好就推 鹏博资讯网站排名top排行榜
  • 天津网络网站制作公司哪家网络推广好
  • 好看的wordpress引导页关键词优化报价怎么样
  • 如何查外贸网站外链网络推广怎么收费
  • 新闻网站模板html网推拉新app推广平台
  • 辽宁建设厅规划设计网站百度竞价推广开户价格
  • 江宁外贸网站建设cilimao磁力猫
  • 湖北建设银行官方网站首页媒体公关公司
  • 做美团团购网站网络营销pdf
  • php网站开发费用企业网址搭建
  • 网站建设方案书ppt软文推广文章范文
  • 南通做电力的公司网站百度官方认证
  • 重庆做商城网站设计杭州百家号优化
  • 怎么建设网站最便宜网站后台管理系统
  • 网站建设注意什么如何让百度搜索排名靠前
  • 网站是什么的集合搜索引擎营销成功的案例
  • ui网站界面设计模板百度软件下载
  • 腾讯云 建网站十大计算机培训学校
  • pos机做网站推广杭州百度开户
  • 天津网站建设 易尔通考研培训班哪个机构比较好
  • wordpress多功能博客西安seo公司哪家好
  • ui和平面设计哪个更有发展杭州seook优屏网络
  • 做营销网站公司网站优化快速排名软件
  • 山东广播电视台惠州优化怎么做seo
  • 社保网站做的真烂外贸推广平台排名
  • wordpress展示产品seo培训班
  • 网易企业邮箱价格在线seo优化
  • 深圳建英文网站seo推广平台服务