Network Programming Learning

Source: Internet
Author: User

1. Network Programming in blocking and non-blocking Modes

Synchronous (or blocking) "and" Asynchronous (or non-blocking )"

Synchronization and Asynchronization are usually for a function,

"Synchronization" means that the function is returned only when all the functions to be executed are completed,

While "Asynchronous" means that a function only performs some simple work and then returns immediately. The function to be implemented is left to other threads or functions for completion. ,

 

2. Why asynchronous I/0?

In a network application that uses synchronous I/O, multithreading is required to process requests from multiple clients at the same time or to communicate with multiple servers at the same time. That is to say, each client request is allocated to a thread for separate processing. Although this can meet our requirements, it will also bring about another problem. Each time a thread is created, a certain amount of memory space (also called the working memory) is allocated to the thread, and the operating system itself limits the total number of threads. If the client has too many requests, the server program may reject the client's requests because it is overwhelmed, or even the server may be paralyzed.

 

 

3. Differences between synchronous I/O and asynchronous

If the data transmission in synchronous I/O mode is compared to the sporadic data transmission mode (here, sporadic data transmission is carried out in sporadic bytes ), data transmission in non-blocking I/O mode can be compared to the data transmission container mode (a buffer layer is added between the byte and the Low-layer data transmission. Therefore, the buffer zone can be seen as a container loaded with bytes)

The same is true during data transmission. If the data volume is small, it is more suitable to use the synchronous I/O mode. If the data volume is large (usually in the unit of G ), non-blocking I/O is more efficient. Therefore, theoretically, the larger the data volume, the lower the unit cost of using the non-blocking I/O method.

 

 

Create a buffer

Java provides seven basic buffers managed by seven classes, which can be found in the Java. Nio package. The seven classes are as follows:

  • Bytebuffer
  • Protocol Buffer
  • Intbuffer
  • Charbuffer
  • Floatbuffer
  • Doublebuffer
  • Longbuffer

 

Their return values or parameters correspond to the corresponding simple type. For example, the get method of the bytebuffer class returns the data of the byte type. The put method requires a parameter of the byte type.

None of these seven classes have public constructor methods. Therefore, they cannot use new to create corresponding object instances. These classes can be used to create corresponding object instances in two ways.

  1. Use static allocate to create a buffer

Public static bytebuffer allocate (INT capacity)


  1. Use the static method wrap to create a buffer

Public static bytebuffer wrap (byte [] array)
Public static bytebuffer wrap (byte [] array, int offset, int length)

 

The array parameter in the wrap method is the array to be converted.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.