Skip to content

Commit 047720e

Browse files
committed
doc update
1 parent f4650a7 commit 047720e

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

README.md

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,31 @@ such as reading or writing
8484
* sender may write 20 bytes to a socket, and the receiver gets only 3 when invoking `read()`
8585
- remaining part may still be in transit
8686

87+
# SelectionKey
88+
* a key represents the registration of a particular channel object with a
89+
particular selector object - for example we have methods: `channel()`, `selector()`
90+
* `SelectionKey` object contains two sets
91+
* the interest set - operations we are interested in
92+
* the ready set - operations the channel is ready to perform (time the selector last checked the states
93+
of the registered channels)
94+
* operations:
95+
* `isReadable()`,
96+
* `isWritable()`,
97+
* `isConnectable()`,
98+
* `isAcceptable()`
99+
* we should use one selector for all selectable channels and delegate the servicing of ready channels to other threads
100+
* therefore we have a single point to monitor channel readiness and a decoupled pool of worker threads to handle
101+
the incoming data
102+
87103
# Selectors
88104
* provide the ability to do readiness selection, which enables multiplexed I/O
89105
* I/O multiplexing is the capability to tell the kernel that we want to be notified if one or more I/O conditions
90106
are ready, like input is ready to be read
91-
* controls the selection process for the channels registered with it
107+
* 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+
* manages information about a set of registered channels and their readiness states
110+
* channels are registered with selectors, and a selector can be asked to update the readiness states of the
111+
channels currently registered with it
92112
* simple analogy
93113
* each pneumatic tube (channel) is connected to a single teller station inside the bank
94114
* station has three slots where the carriers (data buffers) arrive, each with an indicator (selection key) that
@@ -97,51 +117,34 @@ such as reading or writing
97117
to determine if any of the channels are ready (readiness selection)
98118
* teller (worker thread) can perform another task while the drive-through lanes (channels) are idle yet
99119
still respond to them in a timely manner when they require attention
100-
* Selection keys remember what you are interested in for each channel
101-
* track the operations of interest that their channel is currently ready to perform
102120
* invoking `select()` on a selector object causes that the associated keys are updated by checking all the channels
103121
registered with that selector
104-
* by iterating over these keys, you can service each channel that has become ready since the last time you invoked
122+
* by iterating over these keys, we can service each channel that has become ready since the last time we invoked
105123
`select()`
106-
* selectors provide the capability to ask a channel if it's ready to perform an I/O operation of interest to you
107-
* for example - check if `ServerSocketChannel` has any incoming connections ready to accept
108124
* large number of channels can be checked for readiness simultaneously
109125
* true readiness selection is performed by operating system
110126
* One of the most important functions performed by an operating system is to handle
111127
I/O requests and notify processes when their data is ready
112128
* abstractions by which Java code can request readiness selection service from the
113129
underlying operating system
114-
* manages information about a set of registered channels and their
115-
readiness states
116-
* Channels are registered with selectors, and a selector can be asked to
117-
update the readiness states of the channels currently registered with it
118-
* SelectionKey encapsulates the registration relationship between a specific channel
119-
and a specific selector
120-
* SelectionKey objects contain two bit sets (encoded as integers) indicating which
121-
channel operations the registrant has an interest in and which operations the channel is
122-
ready to perform.
123130
* given channel can be registered with more than one selector and has no idea which
124-
Selector objects it's currently registered with.
125-
* Selectors are the managing objects, not the selectable channel objects.
126-
The Selector object performs readiness selection of channels registered
127-
with it and manages selection keys.
131+
`Selector` objects it's currently registered with.
128132
* data never passes through them
129133
* maintains three sets of keys:
130134
* Registered key set
131135
* currently registered keys associated with the selector
132-
* not every registered key is necessarily still valid.
133-
* returned by the keys( ) method
136+
* not every registered key is necessarily still valid
137+
* returned by the `keys()` method
134138
* Selected key set
135139
* `Selected key set c Registered key set`
136-
* key whose associated channel was determined by the selector to be ready
137-
for at least one of the operations in the key's interest set.
138-
* returned by the selectedKeys( )
139-
* selected key set vd the ready set
140-
* Each key has an embedded ready set that indicates the set of operations the
141-
associated channel is ready to perform
140+
* key whose associated channel was determined by the selector to be ready for at least one of the
141+
operations in the key's interest set
142+
* 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
142145
* Cancelled key set
143146
* `Cancelled key set c Registered key set`
144-
* contains keys whose cancel( ) methods have been called (the key has been invalidated),
147+
* contains keys whose `cancel()` methods have been called (the key has been invalidated),
145148
but they have not been deregistered
146149
* following three steps are performed:
147150
1. The cancelled key set is checked
@@ -162,27 +165,4 @@ registered with that selector
162165
ready. But it can return 0 if the wakeup( ) method of the selector is invoked by another thread.
163166
* `select()` - The return value is not a count of ready channels, but the number of channels
164167
that became ready since the last invocation of select( ).
165-
* wakeup( ), provides the capability to gracefully break out a thread from a blocked select( ) invocation
166-
167-
# SelectionKey
168-
* a key represents the registration of a particular channel object with a
169-
particular selector object.
170-
* `channel()`
171-
* `selector()`
172-
* SelectionKey object contains two sets
173-
* the interest set - operations we are interested in
174-
* the ready set - operations the channel is currently ready to perform
175-
* the time the selector last checked the states of the registered channels
176-
* operations:
177-
* `isReadable()`,
178-
* `isWritable()`,
179-
* `isConnectable()`,
180-
* `isAcceptable()`
181-
* The important part is what happens when a key is not already in the selected set. When at
182-
least one operation of interest becomes ready on the channel, the ready set of the key is
183-
cleared, and the currently ready operations are added to the ready set. The key is then added to
184-
the selected key set
185-
* use one selector for all selectable channels and delegate the servicing
186-
of ready channels to other threads
187-
* you have a single point to monitor channel readiness and a
188-
decoupled pool of worker threads to handle the incoming data.
168+
* wakeup( ), provides the capability to gracefully break out a thread from a blocked select( ) invocation

0 commit comments

Comments
 (0)