同步阻塞I/O

Posted 断了线的风筝~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同步阻塞I/O相关的知识,希望对你有一定的参考价值。

TimeServer:

package netty.chapter2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName TimeServer
 * @Description 同步阻塞I/O的TimeServer
 * @author lihk
 * @Date Jul 18, 2017 10:47:42 AM
 * @version 1.0.0
 */
public class TimeServer {

    public static void main(String[] args) {

        int port = 8090;
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("The time server is started in port:" + port);
            Socket socket = null;
            while (true) {
                socket = server.accept();
                System.out.println("Accept message.");
                new Thread(new TimeServerHandler(socket)).start();

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (server != null) {
                System.out.println("The time server close");
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                server = null;
            }
        }

    }

}

TimeServerHandler:

package netty.chapter2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
/**
 * @ClassName TimeServerHandler
 * @Description 同步阻塞I/O的TimeServerHandler
 * @author lihk
 * @Date Jul 18, 2017 10:38:56 AM
 * @version 1.0.0
 */
public class TimeServerHandler implements Runnable {

    private Socket socket;

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

    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 currentTime = null;
            String body = null;
            while (true) {
                body = in.readLine();
                if (body == null)
                    break;
                System.out.println("The time server receive order : " + body);
                currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";
                out.println(currentTime);

            }
        } 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();
                }
                this.socket = null;
            }

        }

    }

}

TimeClient:

package netty.chapter2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @ClassName TimeClient
 * @Description 同步阻塞I/O的TimeClient
 * @author lihk
 * @Date Jul 18, 2017 10:58:49 AM
 * @version 1.0.0
 */
public class TimeClient {
    
    public static void main(String[] args) {
        int port = 8090;
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            socket = new Socket("127.0.0.1", port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(),true);
            out.println("QUERY TIME ORDER");
            System.out.println("Send order 2 server succeed.");
            String resp = in.readLine();
            System.out.println("Now is:"+resp);
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                out.close();
                out = null;
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in = null;
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                socket = null;
            }
        }
    }

}

BIO主要的问题在于每当一个新的客户端请求连接时,服务器必须创建一个新的线程处理新接入的客户端链路,一个线程只能处理一个客户端连接。在高性能服务器应用领域,往往需要面向成千上万个客户端的并发连接,这种模型显然无法满足高性能、高并发的接入场景。为了改进一线程一连接模型,后来又演进出了一种通过线程池或者消息队列实现1个或者多个线程处理N个客户端的模型,由于它的底层通信机制依然使用同步阻塞的I/O(read(),write()方法),所以被称为“伪异步”。

以上是关于同步阻塞I/O的主要内容,如果未能解决你的问题,请参考以下文章

网络I/O模型---同步异步阻塞非阻塞之惑

同步阻塞I/O

socket阻塞与非阻塞,同步与异步I/O模型

I/O 怎样理解阻塞非阻塞与同步异步的区别?

Linux下5种IO模型以及阻塞/非阻塞/同步/异步区别

I/O 阻塞与非阻塞,同步与异步