Java NIO 是一種處理高並發請求的高效技術,使用非阻塞I/O 和輪詢機制實作:建立NIO Selector 監聽事件;註冊Channel 到Selector,監聽ACCEPT 事件;循環等待事件,處理ACCEPT、 READ、WRITE 事件;ACCEPT 事件處理客戶端連接,建立SocketChannel;READ 事件讀取數據,WRITE 事件回寫資料。
Java 函數使用NIO 處理高並發請求
簡介
#非阻塞I/O (NIO) 是Java 中一種用於處理大量並發請求的高效技術。它透過非同步操作和輪詢機制,有效地利用系統資源,提升系統吞吐量。
步驟
1. 建立 NIO Selector
NIO Selector 用於監聽註冊的 Channel 上的事件。
Selector selector = Selector.open();
2. 註冊 Channel
將 ServerSocketChannel 註冊到 Selector,監聽 ACCEPT 事件。
ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); serverChannel.register(selector, SelectionKey.OP_ACCEPT);
3. 迴圈等待事件
透過 Selector.select() 方法監聽事件。
while (true) { selector.select(); Set<SelectionKey> keys = selector.selectedKeys(); // 处理事件... }
4. 處理 ACCEPT 事件
當 ACCEPT 事件發生時,接受連線並建立 SocketChannel。
if (key.isAcceptable()) { ServerSocketChannel channel = (ServerSocketChannel) key.channel(); SocketChannel clientChannel = channel.accept(); clientChannel.configureBlocking(false); clientChannel.register(selector, SelectionKey.OP_READ); }
實戰案例
以下是一個簡單的 Java NIO Echo 伺服器範例。它監聽客戶端連接,並回顯收到的訊息。
EchoServer.java
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; public class EchoServer { private Selector selector; private ServerSocketChannel serverChannel; private int port; public EchoServer(int port) { this.port = port; } public void start() throws IOException { // 创建 Selector selector = Selector.open(); // 创建 ServerSocketChannel serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); serverChannel.bind(new InetSocketAddress(port)); serverChannel.register(selector, SelectionKey.OP_ACCEPT); // 不断循环等待事件 while (true) { int keysCount = selector.select(); if (keysCount == 0) { continue; } Set<SelectionKey> keys = selector.selectedKeys(); for (SelectionKey key : keys) { try { if (key.isAcceptable()) { handleAccept(key); } else if (key.isReadable()) { handleRead(key); } else if (key.isWritable()) { handleWrite(key); } } catch (IOException e) { e.printStackTrace(); key.cancel(); SocketChannel channel = (SocketChannel) key.channel(); channel.close(); } } keys.clear(); } } private void handleAccept(SelectionKey key) throws IOException { ServerSocketChannel channel = (ServerSocketChannel) key.channel(); SocketChannel clientChannel = channel.accept(); clientChannel.configureBlocking(false); clientChannel.register(selector, SelectionKey.OP_READ); } private void handleRead(SelectionKey key) throws IOException { SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int readBytes = channel.read(buffer); if (readBytes == -1) { channel.close(); return; } buffer.flip(); channel.write(buffer); } private void handleWrite(SelectionKey key) throws IOException { SocketChannel channel = (SocketChannel) key.channel(); channel.write(ByteBuffer.allocate(1024)); } public static void main(String[] args) throws IOException { new EchoServer(9090).start(); } }
以上是Java 函數如何使用 NIO 技術處理高並發請求?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器