1. 程式人生 > 其它 >NIO 阻塞模式與非阻塞模式

NIO 阻塞模式與非阻塞模式

NIO 阻塞模式

程式碼理解

我們先寫好客戶端和服務端程式碼

package c2;


import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class Server {
    public static void main(String[] args) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(16);
        ServerSocketChannel ssc = ServerSocketChannel.open();

        ssc.bind(new InetSocketAddress(8080));

        List<SocketChannel> channels = new ArrayList<>();
        while (true){
            log.debug("連線前---");
            SocketChannel sc = ssc.accept();
            log.debug("已連線---{}",sc);
            channels.add(sc);
            for(SocketChannel channel : channels){
                log.debug("讀取前---{}",channel);
                channel.read(buffer);
                buffer.flip();
                while (buffer.hasRemaining()){
                    byte b = buffer.get();
                    System.out.print((char) b);
                    System.out.println();
                }
                buffer.clear();
                log.debug("讀取後---{}",channel);
            }
        }

    }
}
public class Client {
    public static void main(String[] args) throws IOException {
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost",8080));
        System.in.read();
    }
}

執行服務端 可以看到程式停在了連線前這裡

因為accept預設是阻塞方法
以調式模式執行客戶端讓程式不結束執行 此時卡在了讀取前
說明read也是一個阻塞方法

使用調式的評估表示式 傳送一個訊息

可以看到傳送完後 服務端又等待下一次連線

同時如果我們繼續發訊息會發現無法被讀取 因為accept認為沒有新連線加入 產生了阻塞

所以我們會發現 在阻塞模式下 單執行緒很難正常工作

非阻塞模式

設定服務端為非阻塞模式

        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);//非阻塞模式

完整程式碼

package c2;


import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class Server {
    public static void main(String[] args) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(16);
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);//非阻塞模式

        ssc.bind(new InetSocketAddress(8080));

        List<SocketChannel> channels = new ArrayList<>();
        while (true){
            SocketChannel sc = ssc.accept();//非阻塞模式 就算沒有連線 就會返回null
            if(sc != null){
                log.debug("已連線---{}",sc);
                sc.configureBlocking(false);//非阻塞模式 使read不再阻塞
                channels.add(sc);
            }
            for(SocketChannel channel : channels){
                int read = channel.read(buffer);//如果沒有讀到資料則返回0
                if(read > 0){
                    buffer.flip();
                    while (buffer.hasRemaining()){
                        byte b = buffer.get();
                        System.out.print((char) b);
                        System.out.println();
                    }
                    buffer.clear();
                    log.debug("讀取後---{}",channel);
                }
            }
        }

    }
}

這種方式可以解決問題 但是很明顯關於消耗效能了