1. Synchronous and asynchronous
Synchronization: A task runs after the next task is run
Async: Start a new thread after receiving a task to run this task, basically do not occupy the original thread time
2. Blocking and non-blocking
Blocking: When multiple threads access a single piece of data at the same time, only one thread is accessing it and the other is waiting
Non-blocking: Multiple threads can access the same piece of data at the same time
3.nio and IO and AIO differences
NIO: Synchronous non-blocking
IO: Synchronous-blocking
AIO: Asynchronous non-blocking
Efficiency Io<nio<aio
4.buffer buffer: The corresponding data read and write operations
1) subclasses have Bytebuffer,charbuffer,floatbuffer,doublebuffer,intbuffer,longbuffer,shortbuffer, etc., But no Booleanbuffer (because Boolean only true and false is not good at determining its location)
2) 4 Core properties of buffer buffers
Capacity: capacity, representing the maximum amount of data stored in the buffer
Limit: The limit that indicates the size of the data that can be manipulated in the buffer (data after limit cannot read and write operations)
Position: Position representing the position in the buffer where the data is being manipulated
Mark: Mark the location of the current position record
3) Buffer method:
Capacity (): return capacity size = equals Set value
Clear (): Reset Accordingly, limit=capacity= set value position=0
Flip (): The position position becomes the data insertion starting point 0,limit becomes the end of the data insertion, the capacity is not changed, the put will generally use flip ()
Array (): Returns a corresponding array
Hasremaining (): Returns whether the upper bound limit has been reached and is typically used with while mates
Limit (int newlimit): Set upper bounds, generally used to calculate length
Position (int newposition): Set Position location
Remaining (): Returns the number of elements remaining from the position position to the upper bound position
Reset (): Sets the position at the mark () Mark position, in which case the reset is used in conjunction with the MAK
Rewind (): Set the position position to 0 and clear the mark mark
Isdirect (): Returns whether it is a direct buffer
IsReadOnly (): Returns whether this buffer is read-only
HasArray (): Whether this buffer returns an accessible array
4) Conversion between buffer subclasses, as ... , the capacity will make the corresponding byte-length transformation
5) Bytebuffer
Methods to get objects
Bytebuffer.allocate (int capacity): Non-direct buffer
Bytebuffer.allocatedirect (int capacity): Direct buffer
Bytebuffer.wrap (byte[] array): Non-direct buffer
Bytebuffer.wrap (byte[] array, int offset, int length): Non-direct buffer
Bytebuffer Common methods:
Get (byte[] DST): Get buffer Array contents
Get (int index): Gets the buffer content at the specified position
Order (): Gets the byte order of this buffer
Put (byte[] src): buffers are put into data by array
Put (byte B): buffers are placed into data by byte
put (int index, byte b): Place byte data in the specified position
6) General use steps
Create a Buffer object to define the initial capacity
Put put into data
Flip () flipping
Get () Get data
Clear () Reset
5. Channel channels: Connect the channel of the data source, the program cannot directly access the data in the channel, must interact with the Bytebuffer to access the data.
1) methods in the interface
Close (): Close of Channel
Isopen (): Returns whether the channel is open or closed
2) Implement Interface common class
Nio
FileChannel Class Datagramchannel class Serversocketchannel class Socketchannel class
Aio
Asynchronousfilechannel Class Asynchronousserversocketchannel Class Asynchronoussocketchannel class
3) NiO
FileChannel class
Common ways to get objects
Fileinputstream.getchannel ()
Fileoutputstream.getchannel ()
Randomaccessfile.getchannel ()
Filechannel.open (path Path, openoption ... options) (Path:paths.get (String URL),
Openoption ... Variable parameters
Subclass Standardopenoption Enum Type
Common values:
Append: Add at end of file
Create: If the file does not exist and the file is created, overwriting is present, this option ignores the CREATE_NEW option
Create_new: Create if not present, error if present
Write: Writing
READ: Reading)
Common methods:
Force (): Forces the content inside the channel to be written to the appropriate storage device, which is useful for saving data instantly when the system crashes
Lock (): Gets the exclusive lock of the channel (exclusive lock: One thread at a time, shared lock: Concurrent access to shared resources, allowing simultaneous access of licensed multiple threads, such as semaphore)
TransferTo (long position, long count, Writablebytechannel target): Writes a file to another file, similar to Transferform
read
(
ByteBuffer
dst)
:
读取内容放入buffer缓冲区
read
(
ByteBuffer
[] dst)
:读取内容放入缓冲区数组,按先后顺序读(scatter)
Read (bytebuffer[] dsts, int offset, int length): reads the buffer of length array from offset and writes
Size (): Returns the file sizes
Truncate (long size): Intercepting files
Write (Bytebuffer src): Writes a file of buffer buffers
Write (bytebuffer[] srcs): Writes a file of buffer buffers array, in its order (gather)
Write (bytebuffer[] dsts, int offset, int length): reads the buffer of length array from offset and writes
General use steps
Creating objects
Create buffer cache
Reads the content loop read to the buffer
Flip () The corresponding rollover
Write the corresponding writing file
Clear () The corresponding reset, re-use buffer
To close the corresponding
Datagramchannel class
Common ways to get objects
Datagramchannel.open ()
Common methods
Bind (socketaddress Local): Bound port
Connect (socketaddress Remote): Connection port and Address
Disconnect (): Disconnected
IsConnected () Determine if it is connected
Receive (Bytebuffer DST): server-side Accept buffer content
Send (Bytebuffer src, socketaddress target): Sends buffer data to the specified address
Register (Selector sel, int ops): Registers a channel with a given selector, returns a key value
Configureblocking (Boolean block): Adjust blocking mode true blocking mode, false non-blocking mode using selectors requires non-blocking mode
General use steps
Creating Client Objects
Set to direct cache
Creating buffers
Cyclic read read
Flip for the corresponding rollover
Send the buffer, enter the appropriate IP address and port
Clear the corresponding reset buffer
The corresponding Closing object
Create the appropriate object on the server side
Set to direct cache
Bind the appropriate port
Creates a selector polling readiness state where the corresponding flip (), receive (buffer), and clear
Removing iterators
Serversocketchannel class
Common ways to get objects:
Serversocketchannel.open ()
Common methods:
Bind (socketaddress Local): Bound port
Register (Selector sel, int ops): Registers a channel with a given selector, returns a key value
Accept (): Accepts buffer contents
Configureblocking (Boolean block): Adjust blocking mode true blocking mode, false non-blocking mode using selectors requires non-blocking mode
Creating a service-side object
Change to direct cache
Binding port
Create selectors and poll-detect
Loop to read and write accordingly
Removing iterators
Socketchannel class
Common methods for getting objects
Socketchannel.open ()
Common methods
Connect (socketaddress remote): Ibid.
IsConnected (): Ibid.
Read (Bytebuffer DST): Ibid.
Write (Bytebuffer src): ibid.
Write (bytebuffer[] srcs): Ibid.
Write (bytebuffer[] srcs, int offset, int length): Ibid.
Configureblocking (Boolean block): Ibid.
General steps
Common channel objects, and connecting IP addresses and ports
Change to direct cache
Creating a Buffer Object
The corresponding loop flip () write (buffer) clear
Close Channel Object
Selector Selector
Common ways to get objects
Selector.open ()
Common methods
Select (): Swap back the number of buffers that are ready
Selectedkeys (): Keyset (selectedkey.op_connect: Connection Ready, Selectedkey.op_accept: Accept ready, Selectedkey.op_read read ready, Selectedkey.op _write write Ready)
Selectnow (): It's not ready now, it's over.
Select (Long timeout): Up to the end of waiting for timeout not ready
Close (): Close Selector
Selectedkey
IsReadable (): Ready to read
IsWritable (): Ready to write ready
Cancel (): Cancel the selector registration
Isacceptable (): Accept Readiness
Isconnectable (): Is the connection ready
Pipe piping
Sinkchannel Write operations
Sourcechannel Read operation
The rest is basically similar
4) AIO
Asynchronousfilechannel class
Common methods for commonly used objects
Asynchronousfilechannel.open ()
Asynchronousfilechannel. Open (Asynchronouschannelgroup Group): Binding thread pool, (thread pool handles two things: processing IO and distributing competition)
Common methods
Force (Boolean MetaData)
Lock ()
Read (Bytebuffer DST, long position)
Size ()
Truncate (long size)
Write (Bytebuffer src, long position)
Close ()
Asynchronousserversocketchannel class
Common object methods commonly used:
Asynchronousserversocketchannel.open ()
Asynchronousserversocketchannel.open (Asynchronouschannelgroup Group)
Common methods:
Accept ()
Bind (SocketAddress Local)
Asynchronoussocketchannel class
Common ways to get objects
Asynchronoussocketchannel. Open ()
Asynchronoussocketchannel. Open (Asynchronouschannelgroup Group)
Common methods
Bind (SocketAddress Local)
Connect (socketaddress remote)
Read (Bytebuffer DST)
Write (Bytebuffer src)
Shutdowninput ()
Shutdownoutput ()
The NIO and AIO of Java Foundation article