IO model

Simply understand, IO model is what kind of channel to send and receive data, IO model largely determines the performance of program communication

Java supports three network programming models: BIO, NIO, and AIO

BIO

BlockingIO, a server implementation that connects one thread at a time, i.e. when a client requests a server, it needs to start a thread for processing, and this thread blocks when it reads data. This is related to accept() and read() methods. Let’s go straight to the code

Note: Synchronization means that all operations are handled by the current thread. Asynchronism, on the other hand, involves handing off operations to other threads, who then return the results

code

Server code

public class BIOserver { public static void main(String[] args) throws IOException { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 1, TimeUnit.SECONDS,new ArrayBlockingQueue<>(8)); ServerSocket ServerSocket = new ServerSocket(6666); While (true){system.out.println (" Thread "+ thread.currentThread ().getName()+" Listening on port 6666......" ); Socket accept = serverSocket.accept(); System.out.println(" Connection established! ); / / a listen to request to start the thread processing threadPoolExecutor. Execute (new Runnable () {@ Override public void the run () { System.out.println(" Start Thread "+ thread.currentThread ().getName()+" process request "); try { sockerHandler(accept); } catch (IOException e) { e.printStackTrace(); }}}); } } public static void sockerHandler(Socket socket) throws IOException { byte[] bytes = new byte[1024]; Println (" Thread "+ thread.currentThread ().getName()+" waiting for connection to send request......" ); InputStream inputStream = socket.getInputStream(); While (true) {// Write the stream to bytes, print it out int read = inputStream.read(bytes); if (read ! = -1) System.out.println(new String(bytes, 0, read)); else { break; }}}}Copy the code

Client code

public class BIOclient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(6666));
        OutputStream stream = socket.getOutputStream();
        String str="Hello,world!";
        stream.write(str.getBytes());
    }
}
Copy the code

run

Start the server first, and as you can see, it blocks on the Accept () method waiting for the client to connect. If there is no connection, it will always block

Next, we hit a breakpoint and start the client to establish a connection to the server. As you can see below, after establishing the connection, the server blocks on the read() method, waiting for data to be read. If the client does not send data, Thread-1 will always block on read()

Let the client break point go down, and the server reads the data and prints it out

Let’s start another client and continue the process above. As you can see, a new thread is created, thread-2. If we start 100 clients, the server will naturally create 100 threads to handle client requests

Analysis of the

As you can see from the above analysis, Accept () blocks while waiting for a connection, and read() blocks while waiting to read data, so we must create a thread for each client, otherwise Accept () blocks will affect reads to other clients.

One connection creates one thread, 100,000 connections create 100,000 threads, which is obviously unreasonable because the server doesn’t have that many resources to allocate to threads. On the other hand, if the thread has no data to read after the connection is established, the read operation will be blocked, resulting in a waste of thread resources.

The root of the whole problem is the blocking of Accept () and read(), known as optimization points, and then NIO

NIO(to be continued)

NIO, also known as new IO, is a set of new and improved I/o features provided by Java starting with JDK1.4. The three core parts of NIO are Channel, Buffer, and Selector

flow

For the so-called IO stream, I have been unable to understand when I began to learn Java IO, why called IO stream? I started to understand it through further study

To understand IO flow, we can use the analogy of water flow. In daily life, water is usually transported from water supply station to residents’ homes through water pipes. We can compare IO flow to water pipes, and the transmitted data is compared to water in water pipes. Data source and data receiver correspond to water supply station and residents respectively.

The so-called input and output streams are actually relative to the current host memory. If you input data from disk or network card to memory, you use an input stream. If you output data from memory to a network card or disk, you use output streams. That is, input memory with input stream, output memory with output stream!

Continue to update tomorrow……