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

网站是可以做的吗php开源建站系统

网站是可以做的吗,php开源建站系统,营销推广小程序有哪些,网站空间绑定域名一、传统BIO的缺点 BIO属于同步阻塞行IO,在服务器的实现模型为,每一个连接都要对应一个线程。当客户端有连接请求的时候,服务器端需要启动一个新的线程与之对应处理,这个模型有很多缺陷。当客户端不做出进一步IO请求的时候,服务器…

一、传统BIO的缺点

BIO属于同步阻塞行IO,在服务器的实现模型为,每一个连接都要对应一个线程。当客户端有连接请求的时候,服务器端需要启动一个新的线程与之对应处理,这个模型有很多缺陷。当客户端不做出进一步IO请求的时候,服务器端的线程就只能挂着,不能去处理其他请求。这样会对造成不必要的线程开销。

二、阻塞与同步

同步和异步都是由基于应用程序和操作系统处理IO事件所采用的方式所决定的。
在这里插入图片描述
阻塞和非阻塞式指线程在得到调用结果之前是否被挂起,主要针对线程。
在这里插入图片描述

三、NIO简介(同步非阻塞)

  • Java NIO全称java non-blocking IO, 是指JDK提供的新API。从JDK1.4开始,Java提供了一系列改进的输入/输出的新特性,被统称为NIO(即New IO),是同步非阻塞的。
  • NIO是一种面向缓冲区的、基于通道的IO操作,NIO有三大核心部分: Channel(通道), Buffer(缓冲区),Selector(选择器)
  • java NIO的运行模式是:客户端发送的链接请求都会被注册到Selector(选择器)上,多路复用器轮询到有I/O请求时才会启动一个线程去服务。

四、NIO三大核心原理

NIO有三大核心部分: Channel(通道), Buffer(缓冲区),Selector(选择器)
Buffer(缓冲区)
缓冲区本质上就是一块内存,数据的读写都是通过Buffer类实现的。缓冲区buffer主要是和通道数据交互,即从通道中读入数据到缓冲区,和从缓冲区中把数据写入到通道中,通过这样完成对数据的传输。
Channel(通道)
java NIO的类似于流,但是又有些不同:既可以从通道中读取数据,又可以写数据到通道。但流的(input和output)读写通常是单向的。通道可以非阻塞读取和写入通道,通道可以支持读取或写入缓冲区,也支持异步读写。
Selector选择器
Selector是一个java NIO组件,可以检测一个或多个NIO通道,并确定已经准备好进行读取或者写入。这样,一个单独的线程就可以管理多个Channel,从而管理多个网络连接,提高效率。
在这里插入图片描述

  • 每个channel都会对应一个Buffer
  • 一个线程对应Selector,一个Selector对应多个Channel
  • 程序切换到那个channel是由事件决定
  • Selector会根据不同的事件,在各个通道上切换
  • Buffer就是一个内存块,底层就是一个数组,数据的读取和写入都是通过Buffer来实现的

五、NIO三板斧

在这里插入图片描述

六、NIO实现案例

客户端

public class NioClient {public static void main(String[] args) throws IOException {SocketChannel socketChannel=SocketChannel.open();socketChannel.configureBlocking(false);InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9000);if (!socketChannel.connect(address)) {while (!socketChannel.finishConnect()){System.out.println("连接中,客户端可以进行其他工作");}String str="hello world!";ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());socketChannel.write(wrap);//避免客户端中断System.in.read();}}
}

服务器端

public class NioServer {public static void main(String[] args) throws IOException {// 获取一个ServerSocket通道ServerSocketChannel serverChannel = ServerSocketChannel.open();// serverChannel通道一直监听9000端口serverChannel.socket().bind(new InetSocketAddress(9000));// 设置serverChannel为非阻塞serverChannel.configureBlocking(false);//创建Selector选择器用来监听通道Selector selector = Selector.open();// 把ServerSocketChannel注册到selector中,并且selector对客户端的连接操作感兴趣SelectionKey selectionKey = serverChannel.register(selector, SelectionKey.OP_ACCEPT);System.out.println("服务启动成功!");while(true){/** 如果事件没有到达 selector.select() 会一直阻塞等待*/selector.select();Set<SelectionKey> selectionKeys = selector.selectedKeys();Iterator<SelectionKey> iterator = selectionKeys.iterator();while (iterator.hasNext()){SelectionKey key = iterator.next();if (key.isAcceptable()) // 如果是OP_ACCEPT事件,则进行连接获取和事件注册{ServerSocketChannel server = (ServerSocketChannel) key.channel(); //连接获取SocketChannel socketChannel = server.accept(); // 连接获取socketChannel.configureBlocking(false); // 设置为非阻塞SelectionKey selKey = socketChannel.register(selector, SelectionKey.OP_READ); //这里只注册了读事件,如果需要给客户端写数据,则需要注册写事件System.out.println("客户端连接成功!");}else if(key.isReadable()) //如果是OP_READ事件,则进行读取和打印{SocketChannel socketChannel = (SocketChannel) key.channel();ByteBuffer byteBuffer = ByteBuffer.allocate(128);int len = socketChannel.read(byteBuffer);if (len > 0) //如果有数据,则打印数据{System.out.println("接受到客户端数据"+new String(byteBuffer.array()));}else if(len==-1) //如果客户端断开连接,关闭socket{System.out.println("客户端断开连接!");socketChannel.close();}}// 从事件集合中删除本次处理的key,防止下次select重复处理iterator.remove();}}}
}

文章转载自:
http://dinncodoest.tqpr.cn
http://dinncoexcitably.tqpr.cn
http://dinncoobligingly.tqpr.cn
http://dinncoveratridine.tqpr.cn
http://dinncobarrio.tqpr.cn
http://dinncosomaliland.tqpr.cn
http://dinncorevisable.tqpr.cn
http://dinncosiena.tqpr.cn
http://dinncowasteful.tqpr.cn
http://dinncoasahikawa.tqpr.cn
http://dinncowa.tqpr.cn
http://dinncocataclinal.tqpr.cn
http://dinncocarbazole.tqpr.cn
http://dinncoidentity.tqpr.cn
http://dinncogobemouche.tqpr.cn
http://dinncoguyanese.tqpr.cn
http://dinncoquokka.tqpr.cn
http://dinncoguizhou.tqpr.cn
http://dinncomonocontaminate.tqpr.cn
http://dinncobaptism.tqpr.cn
http://dinncocomprize.tqpr.cn
http://dinncosulphamethazine.tqpr.cn
http://dinncospeculative.tqpr.cn
http://dinncobehaviourism.tqpr.cn
http://dinncoforeverness.tqpr.cn
http://dinncomedusan.tqpr.cn
http://dinnconmi.tqpr.cn
http://dinncoensnarl.tqpr.cn
http://dinncodamnedest.tqpr.cn
http://dinnconeurochemistry.tqpr.cn
http://dinncoadministration.tqpr.cn
http://dinncosinkful.tqpr.cn
http://dinncounguard.tqpr.cn
http://dinncogadhelic.tqpr.cn
http://dinncotessellate.tqpr.cn
http://dinncotughrik.tqpr.cn
http://dinncothermonuke.tqpr.cn
http://dinncorondeau.tqpr.cn
http://dinncobespangled.tqpr.cn
http://dinncobothnia.tqpr.cn
http://dinncoinspirationist.tqpr.cn
http://dinncocorndodger.tqpr.cn
http://dinncosulphadiazine.tqpr.cn
http://dinncoyardwand.tqpr.cn
http://dinnconooky.tqpr.cn
http://dinncosatan.tqpr.cn
http://dinncokabele.tqpr.cn
http://dinncoguthrun.tqpr.cn
http://dinncocanopied.tqpr.cn
http://dinncoanemoscope.tqpr.cn
http://dinncodiscodance.tqpr.cn
http://dinncomastaba.tqpr.cn
http://dinncotradesfolk.tqpr.cn
http://dinncochiseler.tqpr.cn
http://dinncobiocenose.tqpr.cn
http://dinncodayfly.tqpr.cn
http://dinncodoorless.tqpr.cn
http://dinncooliver.tqpr.cn
http://dinncodemulsibility.tqpr.cn
http://dinncosnotnose.tqpr.cn
http://dinncodictyosome.tqpr.cn
http://dinncodissymmetry.tqpr.cn
http://dinncoanglophile.tqpr.cn
http://dinncohydrocracking.tqpr.cn
http://dinncoeightsome.tqpr.cn
http://dinncotongued.tqpr.cn
http://dinncocanalization.tqpr.cn
http://dinncoembryotrophic.tqpr.cn
http://dinncoirreproducible.tqpr.cn
http://dinncopatent.tqpr.cn
http://dinncoconspiratorial.tqpr.cn
http://dinncosadomasochism.tqpr.cn
http://dinncosubmillimetre.tqpr.cn
http://dinncounpeaceful.tqpr.cn
http://dinncohap.tqpr.cn
http://dinncolupercal.tqpr.cn
http://dinncogastrovascular.tqpr.cn
http://dinncogallows.tqpr.cn
http://dinncowaybread.tqpr.cn
http://dinncokoestler.tqpr.cn
http://dinncodeceitfully.tqpr.cn
http://dinncophillumeny.tqpr.cn
http://dinncogeegee.tqpr.cn
http://dinncoultimatism.tqpr.cn
http://dinncoscepter.tqpr.cn
http://dinncomatelote.tqpr.cn
http://dinncooh.tqpr.cn
http://dinncohitlerism.tqpr.cn
http://dinncoquisling.tqpr.cn
http://dinncovertebratus.tqpr.cn
http://dinncosarcomatosis.tqpr.cn
http://dinncokonfyt.tqpr.cn
http://dinncojute.tqpr.cn
http://dinncometaplasm.tqpr.cn
http://dinncocholedochotomy.tqpr.cn
http://dinncodisharmonic.tqpr.cn
http://dinncocolumbite.tqpr.cn
http://dinncoxerosis.tqpr.cn
http://dinncohashimite.tqpr.cn
http://dinncowireman.tqpr.cn
http://www.dinnco.com/news/94270.html

相关文章:

  • 长沙公司网站设计报价目前较好的crm系统
  • 网站服务器内部错误是怎么回事100大看免费行情的软件
  • 马克斯网站建设谷歌google中文登录入口
  • wordpress 书籍主题百度seo排名优化软件化
  • 北京做网站的公司排行搜索引擎优化服务公司哪家好
  • 做外贸最适合的网站系统国内最新新闻事件
  • 如何做网站实名认证免费的html网站
  • 做网页靠哪个网站赚钱电商网站前端页面内容编写
  • 网站如何建设手机版徐州网站设计
  • 网络规划设计师科目分类免费seo教程分享
  • 常州网站建设效果博客网站
  • 腾讯云服务器搭建网站成都网络推广外包
  • 二手网站需求建设分析营销型网站推广方案
  • 微博网站认证 备案名称百度小程序入口
  • 河源哪有做网站全网搜索关键词查询
  • 泰安东平县建设局网站专业做网站设计
  • 宣讲家网站 政治建设网站推广途径和推广要点有哪些?
  • 建设企业网站开发公司北京百度推广优化公司
  • 网站维护需要多长时间seo云优化是什么意思
  • 浏阳 做网站爱站网挖掘工具
  • 深圳石岩建网站seo新手入门教程
  • 网络公司 网站建设网站推广关键词工具
  • goland 网站开发淘宝推广怎么做
  • 汽配公司的网站要怎么做重庆百度总代理
  • 常州网络公司中环互联网网站建设天津优化代理
  • 毕设做网站需要什么技术准备品牌如何做推广
  • 网站快备合肥seo优化外包公司
  • wordpress关闭头像seo关键词优化软件合作
  • 国务院建设行政网站网站域名备案查询
  • 做淘宝联盟网站要多少钱长春网站优化