网络IO - 阻塞IO

本文探讨了阻塞IO与同步的区别,指出阻塞会阻止代码执行,而同步允许任务在依赖任务完成时进行其他操作。同时,文章还涉及了阻塞在服务端和客户端的应用场景。
摘要由CSDN通过智能技术生成

阻塞与同步的区别:

        阻塞影响代码继续向后执行;

        同步可以不阻塞从任务理解A的完成依赖B的完成,A可以继续做其它的事情(银行办业务,在抽取号之后就可以做其它的事情不用一直站着排队)

阻塞的本质

服务端

package com.zyl.netio.blocking;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws Exception {
        Server server = new Server(9090);
        server.start();
    }

    static class Server {
        private ServerSocket serverSocket;
        private ThreadPoolExecutor executor;

        public Server(int port) {
            try {
                serverSocket = new ServerSocket();
                serverSocket.bind(new InetSocketAddress(port));

                executor = new ThreadPoolExecutor(10, 30, 30, TimeUnit.SECONDS,
                        new ArrayBlockingQueue<>(30));
                System.out.println("服务已经启动...");
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }

        public void start() {
            while (true) {
                try {
                    Socket accept = serverSocket.accept();
                    System.out.println(accept.getInetAddress().getHostAddress() + " 已经连接");
                    executor.execute(new Handle(accept));
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        }
    }

    static class Handle implements Runnable {
        private Socket socketChannel;

        public Handle(Socket socketChannel) {
            this.socketChannel = socketChannel;
        }

        @Override
        public void run() {
            try {
                BufferedReader bufInstream = new BufferedReader(new InputStreamReader(socketChannel.getInputStream()));

                while (true) {
                    String line = null;
                    while (null != (line = bufInstream.readLine())) {
                        System.out.println(line);

                        socketChannel.getOutputStream().write("Hello Client\r\n".getBytes(StandardCharsets.UTF_8));
                        socketChannel.getOutputStream().flush();
                    }
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
    }
}

客户端

package com.zyl.netio.blocking;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socketChannel = new Socket();
            socketChannel.connect(new InetSocketAddress("127.0.0.1", 9090));

            new Thread(() -> {
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socketChannel.getInputStream()));
                    while (true) {
                        String line = null;
                        while (null != (line = bufferedReader.readLine())) {
                            System.out.println(line);
                        }
                    }
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }).start();

            while (true) {
                socketChannel.getOutputStream().write("Hello Server\r\n".getBytes(StandardCharsets.UTF_8));
                Thread.sleep(1000);
            }
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值