NIO系列-02-升級版BIO

聲明

該系列文章由書籍《Netty權(quán)威指南》第二版整理而來。只為記錄學習筆記。
若認為內(nèi)容侵權(quán)請及時通知本人刪除相關(guān)內(nèi)容。

[TOC]

時間服務(wù)器--升級版(線程池)的BIO

服務(wù)端代碼

服務(wù)端主程序

public class TimeServer {

    public static void main(String[] args) {
        int port = 1234;
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("The time server is listening in port : " + port);
            Socket socket = null;
            // 線程池
            ClientHandlerPool singleExecutor = new ClientHandlerPool(50, 10000);
            while (true) {
                socket = server.accept();
                singleExecutor.execute(new TimeServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (server != null) {
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服務(wù)端線程池

public class ClientHandlerPool {

    private ExecutorService executor;

    public ClientHandlerPool(int maxPoolSize, int queueSize) {
        executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), maxPoolSize, 120L,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize));
    }

    public void execute(Runnable task) {
        executor.execute(task);
    }
}

服務(wù)端處理器類

public class TimeServerHandler implements Runnable {

    private Socket socket;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public TimeServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            out = new PrintWriter(this.socket.getOutputStream(), true);
            String body = null;
            while (true) {
                body = in.readLine();
                if (body == null)
                    break;
                out.println(this.sdf.format(new Date()));
            }

        } catch (Exception e) {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                out.close();
                out = null;
            }
            if (this.socket != null) {
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

客戶端代碼

客戶端代碼和上一篇文章“傳統(tǒng)BIO”一致。

總結(jié)

這種升級版BIO模型有如下特點:

  • 服務(wù)端資源開銷可控
  • 當服務(wù)端隊列積滿后,新的連接必須排隊
  • 可能導致所有的新的連接都超時

參考資料: 《Netty權(quán)威指南》第二版

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,337評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,947評論 18 139
  • 本文是Netty文集中“Netty 那些事兒”系列的文章。主要結(jié)合在開發(fā)實戰(zhàn)中,我們遇到的一些“奇奇怪怪”的問題,...
    tomas家的小撥浪鼓閱讀 15,595評論 3 35
  • 小城昨日還烈火,咋知今日已深秋。 滿山層林盡彩染,輕舟碎裂一秋水。 秋風不識秋水情,秋水汪汪早煞人。 輕歌曼舞細點...
    徐一村閱讀 204評論 0 4
  • 一直的一直 對自己說 要堅強 要樂觀 要微笑 要從容 可是 總有那么一些時候 會無助 會茫然 會頹廢 會流淚 當然...
    若水_086閱讀 232評論 11 5