Skip to content

Commit 6297e3d

Browse files
committed
doc update
1 parent 52d648a commit 6297e3d

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

README.md

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ runtime optimizations), so
1111
* applications are no longer CPU bound (spending most of their time executing code)
1212
* they are I/O bound (waiting for data transfers)
1313
* how to adjust number of threads
14-
* CPU intense tasks - adding more threads than cores will have harmful effect on performance,
15-
suppose that processor works at full power and we force it to do context switches
14+
* CPU intense tasks - adding more threads than cores will have harmful effect on performance
15+
* suppose that processor works at full power and we force it to do context switches
1616
* I/O waiting - adding more threads could be beneficial - context switches are not so harmful
1717
if thread is only waiting
1818
* `Nthreads = NCPU * UCPU * (1 + W/C)`
@@ -43,7 +43,7 @@ runtime optimizations), so
4343
* communication with device controllers
4444
* all I/O flows through kernel space
4545
* why disk controller not send directly to the buffer in user space?
46-
* user space (where process lives) is a nonprivileged area: code executing there cannot directly access
46+
* user space (where process lives) is a unprivileged area: code executing there cannot directly access
4747
hardware devices
4848
* block-oriented hardware devices such as disk controllers operate on fixed-size data blocks
4949
* user process may be requesting an oddly sized chunk of data
@@ -62,7 +62,7 @@ runtime optimizations), so
6262
* is a conduit to an I/O service (a hardware device, a file or socket) and provides methods for
6363
interacting with that service
6464
* socket channel objects are bidirectional
65-
* partial transfer - until buffer's `hasRemaining( )` method returns false
65+
* allows partial transfer - until buffer's `hasRemaining( )` method returns false
6666
* cannot be reused - represents a specific connection to a specific I/O service and encapsulates
6767
the state of that connection
6868
* when a channel is closed - connection is lost
@@ -81,12 +81,12 @@ the state of that connection
8181
such as reading or writing
8282
* sockets are stream-oriented, not packet-oriented
8383
* bytes sent will arrive in the same order, but
84-
* sender may write 20 bytes to a socket, and the receiver gets only 3 when invoking `read()`
84+
* sender may write N bytes to a socket, and the receiver gets only K when invoking `read()`
8585
- remaining part may still be in transit
8686

8787
# SelectionKey
8888
* a key represents the registration of a particular channel object with a
89-
particular selector object - for example we have methods: `channel()`, `selector()`
89+
particular selector object - moreover, we have methods: `channel()`, `selector()`
9090
* `SelectionKey` object contains two sets
9191
* the interest set - operations we are interested in
9292
* the ready set - operations the channel is ready to perform (time the selector last checked the states
@@ -96,7 +96,8 @@ such as reading or writing
9696
* `isWritable()`,
9797
* `isConnectable()`,
9898
* `isAcceptable()`
99-
* we should use one selector for all selectable channels and delegate the servicing of ready channels to other threads
99+
* good practice: we should use one selector for all selectable channels and delegate the servicing of ready
100+
channels to other threads
100101
* therefore we have a single point to monitor channel readiness and a decoupled pool of worker threads to handle
101102
the incoming data
102103

@@ -105,15 +106,15 @@ such as reading or writing
105106
* I/O multiplexing is the capability to tell the kernel that we want to be notified if one or more I/O conditions
106107
are ready, like input is ready to be read
107108
* provide the capability to ask a channel if it's ready to perform an I/O operation of interest to you
108-
* for example - check if `ServerSocketChannel` has any incoming connections ready to accept
109+
* for example - check if `ServerSocketChannel` has any incoming connections ready to be accepted
109110
* manages information about a set of registered channels and their readiness states
110111
* channels are registered with selectors, and a selector can be asked to update the readiness states of the
111112
channels currently registered with it
112113
* simple analogy
113114
* each pneumatic tube (channel) is connected to a single teller station inside the bank
114115
* station has three slots where the carriers (data buffers) arrive, each with an indicator (selection key) that
115116
lights up when the carrier is in the slot
116-
* teller (worker thread) once for a couple of minutes glances up at the indicator lights (invokes select( ))
117+
* teller (worker thread) once for a couple of minutes glances up at the indicator lights (invokes `select()`)
117118
to determine if any of the channels are ready (readiness selection)
118119
* teller (worker thread) can perform another task while the drive-through lanes (channels) are idle yet
119120
still respond to them in a timely manner when they require attention
@@ -123,13 +124,13 @@ registered with that selector
123124
`select()`
124125
* large number of channels can be checked for readiness simultaneously
125126
* true readiness selection is performed by operating system
126-
* One of the most important functions performed by an operating system is to handle
127+
* one of the most important functions performed by an operating system is to handle
127128
I/O requests and notify processes when their data is ready
128129
* abstractions by which Java code can request readiness selection service from the
129130
underlying operating system
130131
* given channel can be registered with more than one selector and has no idea which
131-
`Selector` objects it's currently registered with.
132-
* data never passes through them
132+
`Selector` objects it's currently registered with
133+
* data never passes through selectors
133134
* maintains three sets of keys:
134135
* Registered key set
135136
* currently registered keys associated with the selector
@@ -140,24 +141,14 @@ registered with that selector
140141
* key whose associated channel was determined by the selector to be ready for at least one of the
141142
operations in the key's interest set
142143
* returned by the `selectedKeys()`
143-
* selected key set vs the key's ready set
144-
* each key has an embedded ready set, and each key can be in selected key set
144+
* selected key set vs the key's ready set
145+
* each key has an embedded ready set, and each key can be in selected key set
145146
* Cancelled key set
146147
* `Cancelled key set c Registered key set`
147148
* contains keys whose `cancel()` methods have been called (the key has been invalidated),
148149
but they have not been deregistered
149-
* following three steps are performed:
150-
1. cancelled key set is checked
151-
* each key in the cancelled set is removed from all three sets
152-
* the channel associated with the cancelled key is deregistered
153-
2. operation interest sets of each key in the registered key set are examined
154-
* underlying operating system is queried to determine the actual readiness state
155-
of each channel for its operations of interest
156-
* when a key is not already in the selected set - it is added to the selected key set
157-
3. Step 1 are repeated to complete deregistration of any
158-
channels whose keys were cancelled while the selection operation was in progress.
159150
* `selector.select()`
160-
* blocks indefinitely if no channels are ready.
151+
* blocks indefinitely if no channels are ready
161152
* this method returns a nonzero value since it blocks until a channel is ready
162153
* it can return 0 if the `wakeup()` method of the selector is invoked
163154
* `select()` - return value is not a count of ready channels, but the number of channels

0 commit comments

Comments
 (0)