@@ -84,11 +84,31 @@ such as reading or writing
84
84
* sender may write 20 bytes to a socket, and the receiver gets only 3 when invoking ` read() `
85
85
- remaining part may still be in transit
86
86
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
+
87
103
# Selectors
88
104
* provide the ability to do readiness selection, which enables multiplexed I/O
89
105
* I/O multiplexing is the capability to tell the kernel that we want to be notified if one or more I/O conditions
90
106
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
92
112
* simple analogy
93
113
* each pneumatic tube (channel) is connected to a single teller station inside the bank
94
114
* 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
97
117
to determine if any of the channels are ready (readiness selection)
98
118
* teller (worker thread) can perform another task while the drive-through lanes (channels) are idle yet
99
119
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
102
120
* invoking ` select() ` on a selector object causes that the associated keys are updated by checking all the channels
103
121
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
105
123
` 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
108
124
* large number of channels can be checked for readiness simultaneously
109
125
* true readiness selection is performed by operating system
110
126
* One of the most important functions performed by an operating system is to handle
111
127
I/O requests and notify processes when their data is ready
112
128
* abstractions by which Java code can request readiness selection service from the
113
129
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.
123
130
* 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.
128
132
* data never passes through them
129
133
* maintains three sets of keys:
130
134
* Registered key set
131
135
* 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
134
138
* Selected key set
135
139
* ` 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
142
145
* Cancelled key set
143
146
* ` 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),
145
148
but they have not been deregistered
146
149
* following three steps are performed:
147
150
1 . The cancelled key set is checked
@@ -162,27 +165,4 @@ registered with that selector
162
165
ready. But it can return 0 if the wakeup( ) method of the selector is invoked by another thread.
163
166
* ` select() ` - The return value is not a count of ready channels, but the number of channels
164
167
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