Please enable JavaScript.
Coggle requires JavaScript to display documents.
IO (Java NIO (Buffer (primitive type buffers, MappedByteBuffer, flip() -…
IO
Java NIO
Channel
-
like a stream of data, but support both read & write
-
-
-
Buffer
-
-
-
-
compact() - clean only the data you have read, shift unread data to the beginning of buffer
-
-
-
Selector
-
-
SelectableChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE) - register a channel to the given selector,
2nd arg is when event is interested in (OP_CONNECT, OP_ACCEPT, OP_READ, OP_WRITE)
the channel MUST be in Non-Blocking mode (channel.configureBlocking(false))
FileChannel cannot be in non-blocking mode, hence not available for selector
-
select a chan
-
-
-
selectedKeys() - once the select methods return > 0, get a Set<SelectionKey> of ready selections
use iterator to iterate thru the selKeys. MUST remove each selKey by iterator.remove() after iterating it.
-
-
close() - close the selector, invalidates all selectionKeys registered. Channels are not closed
-
Scatter/Gather
Scatter
-
-
once a buffer is full, move on reading into next buffer
NOT for dynamic data length, need to know the fixed size beforehand
-
Models
Synchronous
Blocking
Order a BigMac and wait at counter until it's ready
Non-Blocking
Order a BigMac and go back to seat, but has to get back to counter every 5 min to see if it's ready
Signal-based
Order a BigMac and go back to seat, staff will notify you when it's ready, but you have to go to counter to pick it yourself
multiplexing
Order a BigMac, get someone wait at counter for you and go back to seat. He'll call you when it's ready, but you have to go to counter to pick it yourself
Asynchronous
Order a BigMac and go back to seat, when it's ready staff will deliver it to your desk
-
-