什麼是同步?什麼是異步?阻塞和非阻塞又有什麼區別?本文先從 Unix 的 I/O 模型講起,介紹了5種常見的 I/O 模型。而後再引出 Java 的 I/O 模型的演進過程,並用實例說明如何選擇合適的 Java I/O 模型來提高系統的並發量和可用性。
由於,Java 的 I/O 依賴於操作系統的實現,所以先了解 Unix 的 I/O 模型有助於理解 Java 的 I/O。
描述的是用戶線程與內核的交互方式:
描述的是用戶線程調用內核 I/O 操作的方式:
一個 I/O 操作其實分成了兩個步驟:發起 I/O 請求和實際的 I/O 操作。 阻塞 I/O 和非阻塞 I/O 的區別在於第一步,發起 I/O 請求是否會被阻塞,如果阻塞直到完成那麼就是傳統的阻塞 I/O ,如果不阻塞,那麼就是非阻塞 I/O 。 同步 I/O 和異步 I/O 的區別就在於第二個步驟是否阻塞,如果實際的 I/O 讀寫阻塞請求進程,那麼就是同步 I/O 。
Unix 下共有五種 I/O 模型:
請求無法立即完成則保持阻塞。


一般很少直接使用這種模型,而是在其他 I/O 模型中使用非阻塞 I/O 這一特性。這種方式對單個 I/O 請求意義不大,但給 I/O 多路復用鋪平了道路.
I/O 多路復用會用到 select 或者 poll 函數,這兩個函數也會使進程阻塞,但是和阻塞 I/O 所不同的的,這兩個函數可以同時阻塞多個 I/O 操作。而且可以同時對多個讀操作,多個寫操作的 I/O 函數進行檢測,直到有數據可讀或可寫時,才真正調用 I/O 操作函數。

從流程上來看,使用 select 函數進行 I/O 請求和同步阻塞模型沒有太大的區別,甚至還多了添加監視 socket,以及調用 select 函數的額外操作,效率更差。但是,使用 select 以後最大的優勢是用戶可以在一個線程內同時處理多個 socket 的 I/O 請求。用戶可以注冊多個 socket,然後不斷地調用 select 讀取被激活的 socket,即可達到在同一個線程內同時處理多個 I/O 請求的目的。而在同步阻塞模型中,必須通過多線程的方式才能達到這個目的。
I/O 多路復用模型使用了 Reactor 設計模式實現了這一機制。
調用 select / poll 該方法由一個用戶態線程負責輪詢多個 socket,直到某個階段1的數據就緒,再通知實際的用戶線程執行階段2的拷貝。 通過一個專職的用戶態線程執行非阻塞I/O輪詢,模擬實現了階段一的異步化
首先我們允許 socket 進行信號驅動 I/O,並安裝一個信號處理函數,進程繼續運行並不阻塞。當數據准備好時,進程會收到一個 SIGIO 信號,可以在信號處理函數中調用 I/O 操作函數處理數據。

調用 aio_read 函數,告訴內核描述字,緩沖區指針,緩沖區大小,文件偏移以及通知的方式,然後立即返回。當內核將數據拷貝到緩沖區後,再通知應用程序。

異步 I/O 模型使用了 Proactor 設計模式實現了這一機制。
告知內核,當整個過程(包括階段1和階段2)全部完成時,通知應用程序來讀數據.
前四種模型的區別是階段1不相同,階段2基本相同,都是將數據從內核拷貝到調用者的緩沖區。而異步 I/O 的兩個階段都不同於前四個模型。
同步 I/O 操作引起請求進程阻塞,直到 I/O 操作完成。異步 I/O 操作不引起請求進程阻塞。

在了解了 UNIX 的 I/O 模型之後,其實 Java 的 I/O 模型也是類似。
在上一節 Socket 章節中的 EchoServer 就是一個簡單的阻塞 I/O 例子,服務器啟動後,等待客戶端連接。在客戶端連接服務器後,服務器就阻塞讀寫取數據流。
EchoServer 代碼:
public class EchoServer {
public static int DEFAULT_PORT = 7;
public static void main(String[] args) throws IOException {
int port;
try {
port = Integer.parseInt(args[0]);
} catch (RuntimeException ex) {
port = DEFAULT_PORT;
}
try (
ServerSocket serverSocket =
new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ port + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
使用多線程來支持多個客戶端來訪問服務器。
主線程 MultiThreadEchoServer.java
public class MultiThreadEchoServer {
public static int DEFAULT_PORT = 7;
public static void main(String[] args) throws IOException {
int port;
try {
port = Integer.parseInt(args[0]);
} catch (RuntimeException ex) {
port = DEFAULT_PORT;
}
Socket clientSocket = null;
try (ServerSocket serverSocket = new ServerSocket(port);) {
while (true) {
clientSocket = serverSocket.accept();
// MultiThread
new Thread(new EchoServerHandler(clientSocket)).start();
}
} catch (IOException e) {
System.out.println(
"Exception caught when trying to listen on port " + port + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
處理器類 EchoServerHandler.java
public class EchoServerHandler implements Runnable {
private Socket clientSocket;
public EchoServerHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
存在問題:每次接收到新的連接都要新建一個線程,處理完成後銷毀線程,代價大。當有大量地短連接出現時,性能比較低。
針對上面多線程的模型中,出現的線程重復創建、銷毀帶來的開銷,可以采用線程池來優化。每次接收到新連接後從池中取一個空閒線程進行處理,處理完成後再放回池中,重用線程避免了頻率地創建和銷毀線程帶來的開銷。
主線程 ThreadPoolEchoServer.java
public class ThreadPoolEchoServer {
public static int DEFAULT_PORT = 7;
public static void main(String[] args) throws IOException {
int port;
try {
port = Integer.parseInt(args[0]);
} catch (RuntimeException ex) {
port = DEFAULT_PORT;
}
ExecutorService threadPool = Executors.newFixedThreadPool(5);
Socket clientSocket = null;
try (ServerSocket serverSocket = new ServerSocket(port);) {
while (true) {
clientSocket = serverSocket.accept();
// Thread Pool
threadPool.submit(new Thread(new EchoServerHandler(clientSocket)));
}
} catch (IOException e) {
System.out.println(
"Exception caught when trying to listen on port " + port + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
存在問題:在大量短連接的場景中性能會有提升,因為不用每次都創建和銷毀線程,而是重用連接池中的線程。但在大量長連接的場景中,因為線程被連接長期占用,不需要頻繁地創建和銷毀線程,因而沒有什麼優勢。
“阻塞I/O+線程池”網絡模型雖然比”阻塞I/O+多線程”網絡模型在性能方面有提升,但這兩種模型都存在一個共同的問題:讀和寫操作都是同步阻塞的,面對大並發(持續大量連接同時請求)的場景,需要消耗大量的線程來維持連接。CPU 在大量的線程之間頻繁切換,性能損耗很大。一旦單機的連接超過1萬,甚至達到幾萬的時候,服務器的性能會急劇下降。
而 NIO 的 Selector 卻很好地解決了這個問題,用主線程(一個線程或者是 CPU 個數的線程)保持住所有的連接,管理和讀取客戶端連接的數據,將讀取的數據交給後面的線程池處理,線程池處理完業務邏輯後,將結果交給主線程發送響應給客戶端,少量的線程就可以處理大量連接的請求。
Java NIO 由以下幾個核心部分組成:
要使用 Selector,得向 Selector 注冊 Channel,然後調用它的 select()方法。這個方法會一直阻塞到某個注冊的通道有事件就緒。一旦這個方法返回,線程就可以處理這些事件,事件的例子有如新連接進來,數據接收等。
主線程 NonBlokingEchoServer.java
public class NonBlokingEchoServer {
public static int DEFAULT_PORT = 7;
public static void main(String[] args) throws IOException {
int port;
try {
port = Integer.parseInt(args[0]);
} catch (RuntimeException ex) {
port = DEFAULT_PORT;
}
System.out.println("Listening for connections on port " + port);
ServerSocketChannel serverChannel;
Selector selector;
try {
serverChannel = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(port);
serverChannel.bind(address);
serverChannel.configureBlocking(false);
selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException ex) {
ex.printStackTrace();
return;
}
while (true) {
try {
selector.select();
} catch (IOException ex) {
ex.printStackTrace();
break;
}
Set<SelectionKey> readyKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = readyKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
try {
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel client = server.accept();
System.out.println("Accepted connection from " + client);
client.configureBlocking(false);
SelectionKey clientKey = client.register(selector,
SelectionKey.OP_WRITE | SelectionKey.OP_READ);
ByteBuffer buffer = ByteBuffer.allocate(100);
clientKey.attach(buffer);
}
if (key.isReadable()) {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer output = (ByteBuffer) key.attachment();
client.read(output);
}
if (key.isWritable()) {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer output = (ByteBuffer) key.attachment();
output.flip();
client.write(output);
output.compact();
}
} catch (IOException ex) {
key.cancel();
try {
key.channel().close();
} catch (IOException cex) {
}
}
}
}
}
}
Java SE 7 版本之後,引入了異步 I/O (NIO.2) 的支持,為構建高性能的網絡應用提供了一個利器。
主線程 AsyncEchoServer.java
public class AsyncEchoServer {
public static int DEFAULT_PORT = 7;
public static void main(String[] args) throws IOException {
int port;
try {
port = Integer.parseInt(args[0]);
} catch (RuntimeException ex) {
port = DEFAULT_PORT;
}
ExecutorService taskExecutor = Executors.newCachedThreadPool(Executors.defaultThreadFactory());
// create asynchronous server socket channel bound to the default group
try (AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open()) {
if (asynchronousServerSocketChannel.isOpen()) {
// set some options
asynchronousServerSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 4 * 1024);
asynchronousServerSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
// bind the server socket channel to local address
asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
// display a waiting message while ... waiting clients
System.out.println("Waiting for connections ...");
while (true) {
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel
.accept();
try {
final AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture
.get();
Callable<String> worker = new Callable<String>() {
@Override
public String call() throws Exception {
String host = asynchronousSocketChannel.getRemoteAddress().toString();
System.out.println("Incoming connection from: " + host);
final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
// transmitting data
while (asynchronousSocketChannel.read(buffer).get() != -1) {
buffer.flip();
asynchronousSocketChannel.write(buffer).get();
if (buffer.hasRemaining()) {
buffer.compact();
} else {
buffer.clear();
}
}
asynchronousSocketChannel.close();
System.out.println(host + " was successfully served!");
return host;
}
};
taskExecutor.submit(worker);
} catch (InterruptedException | ExecutionException ex) {
System.err.println(ex);
System.err.println("\n Server is shutting down ...");
// this will make the executor accept no new threads
// and finish all existing threads in the queue
taskExecutor.shutdown();
// wait until all threads are finished
while (!taskExecutor.isTerminated()) {
}
break;
}
}
} else {
System.out.println("The asynchronous server-socket channel cannot be opened!");
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
本章例子的源碼,可以在 https://github.com/waylau/essential-java 中 com.waylau.essentialjava.net.echo 包下找到。
問啊-定制化IT教育平台,牛人一對一服務,有問必答,開發編程社交頭條 官方網站:www.wenaaa.com
QQ群290551701 聚集很多互聯網精英,技術總監,架構師,項目經理!開源技術研究,歡迎業內人士,大牛及新手有志於從事IT行業人員進入!