NIO: synchronous and asynchronous non-blocking buffer blocking buffer

The introduction of NIO

Multiple high-speed non-blocking network IO channel

Synchronous and asynchronous

The concept is to get its way in terms of procedures for operating results:

Synchronization: Before you get no results or by way of polling constantly asking whether a result, will not return before getting the results

Asynchronous: The caller issued after after the call, a direct result of no return, caller waiting to be produced by the state's result notification call has been completed, the results returned by the callback function

 

Blocking and non-blocking

Blocking: When the implementation process, the results have not been before, this process is temporarily suspended, do nothing

Non-blocking: Before you did not get the results, the thread will not stop the operation, that does not block the current thread;

 

Buffer class (buffer)

Concept: Buffer is an object, it has some basic types of array package. NIO started with Channel ( channel ) is through
Buffer to read and write data. (It is the essence of the array)

Steps for usage

Use Buffer generally follow the write data following four steps:
1. The data is written to Buffer ;
2. call Flip () method; (the limit = position, position = 0, discards Mark) to change the buffer write mode
3. from Buffer read data;
4. calls clear () method or a compact () method. (clear: limit = paracity (capacity), position = 0, discards Mark)

Buffer kind of

Buffer have the following main:
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffe

ByteBuffer internal class encapsulates a byte [] array, and the array can be operated by a number of methods.
Create a ByteBuffer objects
way:

Create a buffer in the heap: the allocate (int Capacity) Capacity buffer capacity
created on the heap buffer called: indirect buffer
way:

In the system memory to create a buffer: allocatDirect (int Capacity)

In the system memory to create a buffer known as: direct buffers
are created and destroyed efficiency indirect buffer than the buffer direct
indirect buffer work less efficient than direct buffers
Three ways:

Through the array to create a buffer: wrap (byte [] arr)
buffer zone is created in this way: indirect buffer

Add elements to the buffer

  1. the ByteBuffer public PUT (byte B) : to add data to the currently available position.
  2. the ByteBuffer public PUT (byte [] byteArray The) : To the position of a currently available byte [] array
  3. the ByteBuffer public PUT (byte [] byteArray The, int offset, int len ) : adding a byte [] part of an array

 

Several parameters buffer

Capacity -capacity
Buffer capacity (Capacity) means: Buffer maximum number of elements can be contained. Defines Buffer , the capacity is not
variable.

Restriction -limit
limit limit refers to: a first element should not read or write the index index. Buffer limit (limit) can not be negative and
must not greater than the capacity.
There are two related methods:
public int limit () : Gets this buffer's limit.
Buffer limit public (int newLimit)
: Set this buffer's limit .

Location -position
position position means: index currently written. A position not less than 0 and not greater than " limit " .
There are two related methods:
public int position () : Get the current position index may be written.
Buffer position public (the p-int)
: change the current write position index.

 

Mark -mark
mark mark means: when calling buffer reset () when the process will buffer the position location is reset to this index. Not
is 0 , can not exceed position .
Related methods:
public Buffer Mark () : This flag is set to the current buffer position location.

 

Common method

Other methods
public int Remaining () : Get position and limit the number of elements between.
boolean isReadOnly public () : Get the current buffer is read-only.
boolean isDirect public () : Gets the current buffer is a direct buffer.

# Common method of
public Buffer the Clear () : reduction of the buffer state.
The position is set to: 0
to limit the limit to the capacity Capacity ;
discard flag Mark .
Buffer Flip public () : Narrow limit range.
The limit to the current position position;
the current position position is set to 0 ;
discard flag.
Buffer rewind public () : This rewind buffer.
The position position is set: 0
restrictions limit unchanged.
Discard flag.

 

Guess you like

Origin www.cnblogs.com/xiaojians/p/12631503.html