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

做导购型网站识图

做导购型网站,识图,南京专业做网站的公司有哪些,wordpress软件无法登陆BIO(Blocking I/O)、NIO(Non-blocking I/O)和AIO(Asynchronous I/O)是Java中用于处理I/O操作的三种不同的编程模型. BIO适用于连接数较少的情况,NIO适用于连接数较多但连接活跃度不高的情况&…

BIO(Blocking I/O)、NIO(Non-blocking I/O)和AIO(Asynchronous I/O)是Java中用于处理I/O操作的三种不同的编程模型.

BIO适用于连接数较少的情况,NIO适用于连接数较多但连接活跃度不高的情况,而AIO适用于连接数较多且连接活跃度较高的情况。选择合适的I/O模型取决于具体的应用场景和性能要求。

以下是他们的各自介绍以及代码示例

  1. BIO(Blocking I/O)
    • 同步阻塞I/O模型,传统的I/O模型。
    • 每个I/O操作都会阻塞当前线程,直到数据读取完成或写入完成。
    • 适用于连接数较少且固定的场景,简单易用。

代码示例:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class BIOServer {public static void main(String[] args) throws IOException {ServerSocket serverSocket = new ServerSocket(8888);System.out.println("BIO Server started on port 8888");while (true) {Socket socket = serverSocket.accept();System.out.println("Accepted connection from " + socket);BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));String message = reader.readLine();System.out.println("Received message: " + message);writer.write("Hello, client!\n");writer.flush();socket.close();}}
}
  1. NIO(Non-blocking I/O)
    • 同步非阻塞I/O模型,提供了Channel和Buffer的概念。
    • 可以使用单线程处理多个连接,提高了I/O的效率。
    • 可以实现多路复用(Selector)来处理多个通道的I/O操作。

代码示例:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;public class NIOServer {public static void main(String[] args) throws IOException {ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();serverSocketChannel.bind(new InetSocketAddress(8888));serverSocketChannel.configureBlocking(false);Selector selector = Selector.open();serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);System.out.println("NIO Server started on port 8888");while (true) {selector.select();Set<SelectionKey> selectedKeys = selector.selectedKeys();Iterator<SelectionKey> keyIterator = selectedKeys.iterator();while (keyIterator.hasNext()) {SelectionKey key = keyIterator.next();if (key.isAcceptable()) {ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();SocketChannel socketChannel = serverChannel.accept();socketChannel.configureBlocking(false);socketChannel.register(selector, SelectionKey.OP_READ);System.out.println("Accepted connection from " + socketChannel);} else if (key.isReadable()) {SocketChannel socketChannel = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(1024);socketChannel.read(buffer);buffer.flip();String message = new String(buffer.array()).trim();System.out.println("Received message: " + message);socketChannel.register(selector, SelectionKey.OP_WRITE);} else if (key.isWritable()) {SocketChannel socketChannel = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.wrap("Hello, client!\n".getBytes());socketChannel.write(buffer);socketChannel.close();System.out.println("Message sent to client");}keyIterator.remove();}}}
}
  1. AIO(Asynchronous I/O)
    • 异步非阻塞I/O模型,提供了异步的I/O操作方式。
    • 使用异步通道(AsynchronousChannel)来处理I/O操作,可以在完成之前继续做其他事情。
    • 适用于连接数较多且连接活跃度较高的场景,如聊天服务器、网络爬虫等。

代码示例:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;public class AIOServer {public static void main(String[] args) throws IOException, InterruptedException {AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open();serverChannel.bind(new InetSocketAddress(8888));System.out.println("AIO Server started on port 8888");serverChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {public void completed(AsynchronousSocketChannel socketChannel, Void attachment) {serverChannel.accept(null, this);ByteBuffer buffer = ByteBuffer.allocate(1024);socketChannel.read(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() {public void completed(Integer result, ByteBuffer buffer) {buffer.flip();String message = new String(buffer.array()).trim();System.out.println("Received message: " + message);ByteBuffer responseBuffer = ByteBuffer.wrap("Hello, client!\n".getBytes());socketChannel.write(responseBuffer, responseBuffer, new CompletionHandler<Integer, ByteBuffer>() {public void completed(Integer result, ByteBuffer buffer) {try {socketChannel.close();} catch (IOException e) {e.printStackTrace();}System.out.println("Message sent to client");}public void failed(Throwable exc, ByteBuffer buffer) {exc.printStackTrace();}});}public void failed(Throwable exc, ByteBuffer buffer) {exc.printStackTrace();}});}public void failed(Throwable exc, Void attachment) {exc.printStackTrace();}});Thread.sleep(Integer.MAX_VALUE);}
}

这些示例演示了如何使用Java的BIO、NIO和AIO来实现简单的Socket通信。 BIO使用阻塞I/O模型,NIO使用非阻塞I/O模型,AIO使用异步I/O模型。

http://www.mmbaike.com/news/22365.html

相关文章:

  • 做网站好听的域名购买链接平台
  • 重庆网站建设 制作 设计 优惠价交换链接适合哪些网站
  • 榆中县城乡建设局网站百家号排名
  • 申请备案网站空间济南seo小黑seo
  • 网站优化检查广告制作
  • WordPress表单系统上海seo培训中心
  • 网站建设程序做哪些全自动引流推广软件
  • 可以做仿真实验的网站博客seo怎么做
  • 网站建设概十大软件免费下载网站排行榜
  • 搭建网站平台如何做分录优秀网站网页设计分析
  • 深圳建设材料价格网站网络销售平台排名
  • 网站开发协议书 英文版泰安seo网络公司
  • 怎么做网站的排名优化网站seo百度百科
  • 如何知道网站用什么程序做的百度推广开户渠道公司
  • 加强网站建设的长沙百度关键词搜索
  • 南京电商网站建设网站制作大概多少钱
  • 建设官方网站商品推广软文800字
  • php网站颜色改变网站推广优化方案
  • 网站怎么做页游网站快速优化排名推荐
  • 怎样做二维码网站网站如何推广运营
  • 做vip兼职设计师的网站有哪些网络公关
  • 建站公司都有哪些市场营销培训课程
  • 怎样注册商标申请seo优化
  • 江阴哪里有做网站推广厦门小鱼网
  • 在线编辑网页宁波seo优化
  • 网站建设学什么企业网站排名优化
  • 搭建网站公司哪家好seo搜索引擎优化简历
  • wordpress 公式编辑器免费推广seo
  • 做导航网站把别人的网址链接过来要经过允许吗产品seo怎么优化
  • 河南做网站 河南网站建设报个电脑培训班要多少钱