preface

In the last article we gave a comprehensive overview of IO and an in-depth look at Java IO (I) overview

Before we get to BIO in this article, let’s review the concepts: synchronous vs. asynchronous, blocking vs. non-blocking.

Synchronous | | non-blocking asynchronous | jam

synchronous

It refers to the coordinated pace. It’s called synergy, so there must be at least two things. The result of synergy is that many things cannot be done at the same time, but must be done one by one, and the last thing ends before the next thing begins.

There are two little things to know about synchronization:

  1. Scope: Desynchronization is required on a global scale, but only at certain critical points. For example, there is only one window for selling food in the canteen, which must be synchronized. One person buys it, and the next person buys it. But does one person eat before the next person starts eating? Of course not.

  2. Granularity: It is not only large – grained things that have synchronization, but also small – grained things. It’s just that smaller-grained synchronization of things is usually naturally supported, whereas larger-grained synchronization often requires manual processing. For example, synchronization between two threads needs to be handled manually, but two statements in a thread are naturally synchronized.

asynchronous

It’s a different pace. Since it is different, that is not the same. So the result is that multiple things can go your way, I can go my way, nobody cares about anybody, and everything is going on at the same time.

Note: Be sure to appreciate "multiple things", multiple threads are multiple things, multiple methods are multiple things, multiple statements are multiple things, and multiple CPU instructions are multiple things.

blocking

So-called blocking: refers to the obstruction of the blockage. It can be understood as the immobility caused by encountering an obstacle.

non-blocking

So-called non-blocking: nature is the opposite of blocking, which can be understood as continuing unimpeded because there is no obstacle.

BIO

BIO is commonly known as synchronous blocking IO, a very traditional IO model. In simple terms, a BIO is a mode of operation in which a connection is served by a dedicated thread on a server. It’s like a restaurant where a guest comes and a dedicated waiter is assigned to this guest, who will only serve this one guest until he leaves.

Example:

BIO means block IO, and if you look at the code below,

Public class BIOServer {public static void main(String[] args) throws IOException {// Create thread pool ExecutorService executorService = Executors.newCachedThreadPool(); // Create serverSocket serverSocket serverSocket = new serverSocket (8888); System.out.println(" Server started, port 8888...") ); While (true){system.out.println (" Thread information id="+ thread.currentThread ().getid ()+ "name ="+ thread.currentThread ().getName()); System.out.println(" wait for connection...") ); // Listen to final Socket Accept = serverSocket.accept(); System.out.println(" There is a client connection...") ); executorService.execute(new Runnable() { @Override public void run() { handler(accept); }}); } } public static void handler(Socket socket){ try { byte[] bytes = new byte[1024]; InputStream = socket.getinputStream (); While (true){system.out.println (" Thread information id="+ thread.currentThread ().getid ()+ "name ="+ thread.currentThread ().getName()); System.out.println("read..." ); int read = inputStream.read(bytes); if(read! =-1){system.out.println (new String(bytes,0,read)); }else { break; } } }catch (Exception e){ e.printStackTrace(); }finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); }}}}Copy the code

Starting the service, the console output:

The server has been started. The port number is 8888... Id =1 name =main wait for connection...Copy the code

Enter Telnet 127.0.0.1 8888 and press Enter.

The server has been started. The port number is 8888... Id =1 name =main wait for connection... There is a client connection... Id =1 name =main wait for connection... Thread id=13 Name =pool-1-thread-1 read...Copy the code

Press CTRL +] to enter; Send 100OK, for example:

Console output:

The server has been started. The port number is 8888... Id =1 name =main wait for connection... There is a client connection... Id =1 name =main wait for connection... Thread id=13 Name =pool-1-thread-1 read... 100OK Thread id=13 Name =pool-1-thread-1 read...Copy the code

Again, open a command line window, type Telnet 127.0.0.1 8888 and press Enter. Console information:

The server has been started. The port number is 8888... Id =1 name =main wait for connection... There is a client connection... Id =1 name =main wait for connection... Thread id=13 Name =pool-1-thread-1 read... 100OK Thread id=13 Name =pool-1-thread-1 read... There is a client connection... Id =1 name =main wait for connection... Id =14 Name =pool-1-thread-2 read...Copy the code

Press CTRL +] to enter; Enter data 200OK, the console information is as follows:

The server has been started. The port number is 8888... Id =1 name =main wait for connection... There is a client connection... Id =1 name =main wait for connection... Thread id=13 Name =pool-1-thread-1 read... 100OK Thread id=13 Name =pool-1-thread-1 read... There is a client connection... Id =1 name =main wait for connection... Id =14 Name =pool-1-thread-2 read... 200OK Thread id=14 Name =pool-1-thread-2 read...Copy the code

Existing problems

  • The accept method cannot return as long as no client has connected to the server. This is blocking. The same applies to read and write operations. If you want to read data, you must wait until the data arrives. This is called blocking.
  • The server creates a separate thread for each request.
  • When the number of concurrent requests is large, a large number of threads need to be created, which consumes a large amount of system resources.
  • After the connection is established, if the current thread has no data to read, the thread blocks on the read, resulting in a waste of thread resources.

At the end

I am a code is being hit is still trying to advance. If the article is helpful to you, remember to like, follow yo, thank you!