NIO common API

 

A, Selector registration event

1.1

SelectionKey.OP_CONNECT

Ready connection (client connects well, need to go through three-way handshake, four wave)

1.2

SelectionKey.OP_ACCEPT

Ready to accept (a new client come)

1.3

SelectionKey.OP_READ

Xu studied (new data arrives)

1.4

SelectionKey.OP_WRITE

Write-ready (you can write data a)

 

Two, Selector using flow

2.1

open()

Open a selector, to the selector you may need to focus on the event registration

2.2

register()

Channel registered to a selector, call back to the Channel when the selector triggers a corresponding attention event, data processing

2.3

select()/selectNow()

Channel a channel, a process currently available, the channel data to be processed

(These two methods are blocking method, blocking until the event reaches the observer .Selector is not a complete model, is a half-observer mode. Available registration and the registration event to the Selector, but when the event arrives and does not notify Channel need to traverse the pond, you need a basic thread in the traversal, it requires at least one thread)

2.4

selectedKeys()

Ready to get the current channel

2.5

wakeUp()

Wake selector is in a select state

2.6

close()

Closing a selector, canceled all events of interest

 

Three, Selector Notes

Register to the selector channel must be unblocked

FileChannel not be used Selector, because FileChannel not switch to non-blocking mode; can socket channel

(FileChannel is just a file channel, the channel may be used to manipulate files, that can be operated using a bulk file may be a buffer into which the data block, a block of data into the file. However, we can not say when the current file readable, please tell me, when the current file writable please tell me, can never read the file)

Interest set (registered simultaneously readable, writable state), the Ready set (the currently set ready, including a set of readable, writable set)

Channel Channel

Selector Selector

obj added value

 

Published 174 original articles · won praise 115 · views 830 000 +

Guess you like

Origin blog.csdn.net/nicolelili1/article/details/103983839