@@ -11,8 +11,8 @@ runtime optimizations), so
11
11
* applications are no longer CPU bound (spending most of their time executing code)
12
12
* they are I/O bound (waiting for data transfers)
13
13
* 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
16
16
* I/O waiting - adding more threads could be beneficial - context switches are not so harmful
17
17
if thread is only waiting
18
18
* ` Nthreads = NCPU * UCPU * (1 + W/C) `
@@ -43,7 +43,7 @@ runtime optimizations), so
43
43
* communication with device controllers
44
44
* all I/O flows through kernel space
45
45
* 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
47
47
hardware devices
48
48
* block-oriented hardware devices such as disk controllers operate on fixed-size data blocks
49
49
* user process may be requesting an oddly sized chunk of data
@@ -62,7 +62,7 @@ runtime optimizations), so
62
62
* is a conduit to an I/O service (a hardware device, a file or socket) and provides methods for
63
63
interacting with that service
64
64
* 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
66
66
* cannot be reused - represents a specific connection to a specific I/O service and encapsulates
67
67
the state of that connection
68
68
* when a channel is closed - connection is lost
@@ -81,12 +81,12 @@ the state of that connection
81
81
such as reading or writing
82
82
* sockets are stream-oriented, not packet-oriented
83
83
* 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() `
85
85
- remaining part may still be in transit
86
86
87
87
# SelectionKey
88
88
* 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() `
90
90
* ` SelectionKey ` object contains two sets
91
91
* the interest set - operations we are interested in
92
92
* 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
96
96
* ` isWritable() ` ,
97
97
* ` isConnectable() ` ,
98
98
* ` 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
100
101
* therefore we have a single point to monitor channel readiness and a decoupled pool of worker threads to handle
101
102
the incoming data
102
103
@@ -105,15 +106,15 @@ such as reading or writing
105
106
* I/O multiplexing is the capability to tell the kernel that we want to be notified if one or more I/O conditions
106
107
are ready, like input is ready to be read
107
108
* 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
109
110
* manages information about a set of registered channels and their readiness states
110
111
* channels are registered with selectors, and a selector can be asked to update the readiness states of the
111
112
channels currently registered with it
112
113
* simple analogy
113
114
* each pneumatic tube (channel) is connected to a single teller station inside the bank
114
115
* station has three slots where the carriers (data buffers) arrive, each with an indicator (selection key) that
115
116
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() ` )
117
118
to determine if any of the channels are ready (readiness selection)
118
119
* teller (worker thread) can perform another task while the drive-through lanes (channels) are idle yet
119
120
still respond to them in a timely manner when they require attention
@@ -123,13 +124,13 @@ registered with that selector
123
124
` select() `
124
125
* large number of channels can be checked for readiness simultaneously
125
126
* 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
127
128
I/O requests and notify processes when their data is ready
128
129
* abstractions by which Java code can request readiness selection service from the
129
130
underlying operating system
130
131
* 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
133
134
* maintains three sets of keys:
134
135
* Registered key set
135
136
* currently registered keys associated with the selector
@@ -140,24 +141,14 @@ registered with that selector
140
141
* key whose associated channel was determined by the selector to be ready for at least one of the
141
142
operations in the key's interest set
142
143
* 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
145
146
* Cancelled key set
146
147
* ` Cancelled key set c Registered key set `
147
148
* contains keys whose ` cancel() ` methods have been called (the key has been invalidated),
148
149
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.
159
150
* ` selector.select() `
160
- * blocks indefinitely if no channels are ready.
151
+ * blocks indefinitely if no channels are ready
161
152
* this method returns a nonzero value since it blocks until a channel is ready
162
153
* it can return 0 if the ` wakeup() ` method of the selector is invoked
163
154
* ` select() ` - return value is not a count of ready channels, but the number of channels
0 commit comments