码迷,mamicode.com
首页 > 其他好文 > 详细

同步阻塞I/O

时间:2017-07-18 13:35:47      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:队列实现   null   query   soc   java   this   客户端连接   amr   []   

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

标签:队列实现   null   query   soc   java   this   客户端连接   amr   []   

原文地址:http://www.cnblogs.com/hunter-56213/p/7199765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!