What is concurrency

TCP/IP connection

The socket

channel

What is maximum concurrency

How many TCP/IP connections there are

How to test

The relationship between concurrency and multithreading

The server side

Blocking and non-blocking

1. Slow block processing //tomcat one by one 2

Single thread and multi-thread

1. Advantages of single thread: fast speed and less space; disadvantages: concurrency problems

One thread per request //tomcat // summary Tomcat is blocking, multithreading

Advantages no concurrency problems disadvantages slow speed more space

The client

Blocking and non-blocking

Single thread and multi-thread

tomcat

1. Older versions block multithreading

2. The new version 8 non-blocking single thread / / maximum number of simultaneous connections to 10000 www.cnblogs.com/doit8791/p/…


AcceptCount maxConnections maxThreads Receives the connection in the Accept queue (when the client sends a request to the server, if the client establishes a connection with the OS after completing a three-way handshake, the OS puts the connection in the Accept queue); Get the requested data in the connection and generate the request; Call the servlet container to process the request; Returns the response.

Corresponding parameters in Connector function as follows:

1. AcceptCount Specifies the length of the accept queue. When the number of connections in the Accept queue reaches the acceptCount, the queue is full and all incoming requests are rejected. The default value is 100.

MaxConnections Maximum number of connections that Tomcat can receive and process at any time. When Tomcat receives maxConnections, the Acceptor thread does not read the connections in the Accept queue. The threads in the Accept queue will block until Tomcat receives fewer connections than maxConnections. If the value is set to -1, the number of connections is unlimited.

The defaults are dependent on the protocol used by the connector: NIO defaults to 10000(ten thousand level), APR/native defaults to 8192, and BIO defaults to maxThreads200 (Executor’s maxThreads if configured).

On Windows, the APR/native maxConnections value is automatically adjusted to an integer multiple of the maximum set value of 1024; If the value is set to 2000, the maximum value is 1024.

MaxThreads Specifies the maximum number of request processing threads. The default is 200 (Tomcat7 and 8 are both) (level 100). If the Connector is bound to executors, this value is ignored because the Connector will use the bound Executor instead of the built-in thread pool to perform tasks.

MaxThreads specifies the maximum number of threads, not the actual number of running cpus. In fact, maxThreads is much larger in size than the number of CPU cores. This is because the thread processing the request may spend very little time actually computing, and most of the time may be blocked, waiting for the database to return data, waiting for data to be read or written to the disk, etc. Thus, at any given moment, only a few threads are actually using the physical CPU, and most threads are waiting; So it makes sense that the number of threads is much greater than the number of physical cores.

In other words, Tomcat can keep the CPU busy by using many more threads than the number of CPU cores, greatly increasing CPU utilization.

www.importnew.com/27309.html


conclusion

Tomcat is server-side.

Nio Netty GRPC serves as a server.

The socket and nio channel

socket

Maximum number of concurrent connections:? Several hundred ~ several thousand depending on the number of threads // the implementation of multithreading creates one thread per connection request


Early solutions to network read-write data.


Advantages Simple to use

Disadvantages slow speed because of blocking. Large space because of multithreading.

nio channel

Maximum number of concurrent connections:? // How to implement the tomcat source code – non-blocking mode // how to test? Since there are only a few thousand threads in a single machine, testing a few thousand client connections is no problem. // How to test 10000 level? A single machine can have tens of thousands of ports, but the JVM has thousands of threads so you can increase the JVM memory, making the number of threads more. Under test. //


The new version is NIO. Nio is a new solution that has nothing to do with sockets. // Channel contains the field -socket object, which gets the remote IP /port and local IP /port of the current connection


Advantages fast because of non-blocking. Less space because of single threads.

Disadvantages: Complex use


additional

Maximum number of Concurrent Tomcat connections?

Tomcat is based on either blocking IO (the number of connections in hundreds is one thread per connection, with hundreds being the upper limit) or non-blocking NIO (the number of connections in tens of thousands, with a single thread handling all connections).

How to test? Find information online.

netty

Maximum number of concurrent connections: million connections per machine.


How to test? Juejin. Cn/post / 684490… 10,000 connections. Optimized to reach 1 million connections. // Why are there so many connections? Because a single thread handles all connections without blocking.


Encapsulates Java NIO. More convenient to use.


advantages

disadvantages


code

Juejin. Cn/post / 684490…

grpc

Cross-language, cross-platform remote services.

The maximum number of netTY-based connections is similar to that of NetTY.


How to test?


Code?


Print log

1000~2000 threads/connections will occur connection reset – client

Warning: RPC failed: Status{code=UNAVAILABLE, description= IO exception, cause=java.io.IOException: Connection reset by peer at sun.nio.ch.FileDispatcherImpl.read0(Native Method) at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) at  sun.nio.ch.IOUtil.read(IOUtil.java:192) at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:384) at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288) at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1128) at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:347) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644) at  io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458) at io.netty.util.concurrent.SingleThreadEventExecutorA $5.run(SingleThreadEventExecutor.java:897)

	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

	at java.lang.Thread.run(Thread.java:745)

}


Copy the code

Same client channel

It only connects once and then it shuts down

[the Console output redirected to file: / Users/gongzhihao/git/repository2 / examples / 2. The log] on February 13, 2019 IO 2:52:36 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: the Greeting: Hello world / / even 1 time off 0 February 13, 2019 IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet world ... February 13, 2019, IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet warning: RPC failed: Status{code=UNAVAILABLE, Description =Channel shutdown invoked, cause= NULL} // Second Channel closed 2 February 13, 2019 IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet warning: RPC failed: Status{code=UNAVAILABLE, Description =Channel shutdown invoked, cause= NULL} 4 February 13, 2019 IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet warning: RPC failed: Status{code=UNAVAILABLE, Description =Channel shutdown invoked, cause= NULL} 6 February 13, 2019 IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:52:37 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet warning: RPC failed: Status{code=UNAVAILABLE, description=Channel shutdown invoked, cause=null} 8Copy the code

Different client channels

Each other 1000000 1000000 no upper limit // Multi-threaded solution: the upper limit is 1000~2000 will report an error – connection reset – client

[the Console output redirected to file: / Users/gongzhihao/git/repository2 / examples / 2. The log] on February 13, 2019 IO 2:58:10 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: the Greeting: Hello world 0 February 13, 2019 IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: the Greeting: Hello world 2 February 13, 2019 IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: the Greeting: Hello world 4 February 13, 2019 IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: the Greeting: Hello world 6 February 13, 2019 IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: Will try to greet the world... February 13, 2019, IO 2:58:11 afternoon. GRPC. Examples. The helloworld., HelloWorldClient greet information: the Greeting: Hello world 8Copy the code

Single-machine – Maximum number of threads in a single JVM process

1000 level 5000 // The JVM memory is insufficient when 5000 threads are created by one JVM process. // The number of 1000 level threads can be created by both the server and client on a single machine


Print log

4000 The JVM is out of memory to create threads


thread name:Thread-4069, thread id:4079

thread name:Thread-4070, thread id:4080

Exception in thread "main"java.lang.OutOfMemoryError: At java.lang.thread. start0(native Method) at java.lang.thread. start0(native Method) at java.lang.Thread.start(Thread.java:717) at demo.NIOClient.main(NIOClient.java:104) java.io.IOException: Too many Open files // A process in Linux: Maximum 1000 file handle https://www.cnblogs.com/kongzhongqijing/articles/3735664.html at sun. Nio. Ch. IOUtil. MakePipe (Native Method) at sun.nio.ch.KQueueSelectorImpl.<init>(KQueueSelectorImpl.java:84) at sun.nio.ch.KQueueSelectorProvider.openSelector(KQueueSelectorProvider.java:42) at java.nio.channels.Selector.open(Selector.java:227) at demo.NIOClient.initClient(NIOClient.java:35) at demo.Task1.run(Task1.java:14) at java.lang.Thread.run(Thread.java:748)Copy the code

The test code

slightly