|
1 |
| -# java12-nio-non-blocking-client-server-workshop |
| 1 | +# java12-nio-non-blocking-selector-server-workshop |
2 | 2 |
|
3 | 3 | _Reference_: https://www.udemy.com/java-non-blocking-io-with-javanio-and-design-patterns/
|
4 | 4 | _Reference_: https://github.com/kabutz/Transmogrifier
|
5 | 5 | _Reference_: http://www.java2s.com/Tutorials/Java/Socket/How_to_use_Java_SocketChannel_create_a_HTTP_client.htm
|
6 | 6 | _Reference_: https://www.youtube.com/watch?v=3m9RN4aDh08
|
7 | 7 |
|
8 |
| -# introduction |
9 |
| -* current JVMs run bytecode at speeds approaching that of natively compiled code or even better (dynamic |
10 |
| -runtime optimizations), so |
11 |
| - * applications are no longer CPU bound (spending most of their time executing code) |
12 |
| - * they are I/O bound (waiting for data transfers) |
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 |
16 |
| - * I/O waiting - adding more threads could be beneficial - context switches are not so harmful |
17 |
| - if thread is only waiting |
18 |
| - * `Nthreads = NCPU * UCPU * (1 + W/C)` |
19 |
| - * NCPU is the number of cores, available through |
20 |
| - `Runtime.getRuntime().availableProcessors()` |
21 |
| - * UCPU is the target CPU utilization (between 0 and 1) |
22 |
| - * W/C is the ratio of wait time to compute time |
23 |
| -* operating system vs Java stream-based I/O model |
24 |
| - * operating system wants to move data in large chunks (buffers) |
25 |
| - * I/O classes of the JVM operates on small pieces — single bytes, or lines of text |
26 |
| - * operating system delivers buffers full of data -> stream classes of `java.io` breaks it down into little pieces |
27 |
| - * NIO provides similar concepts to operating system buffers - `ByteBuffer` object |
28 |
| - * `RandomAccessFile` with array-based `read( )` and `write( )` are pretty close to the underlying |
29 |
| - operating-system calls (although at least one buffer copy) |
30 |
| - |
31 |
| -# Buffer Handling |
32 |
| -* buffers, and how buffers are handled, are the basis of all I/O |
33 |
| -* "input/output" means nothing more than moving data in and out of buffers |
34 |
| -* processes perform I/O by requesting operating system to: |
35 |
| - * write: drain data from a buffer |
36 |
| - * read: fill buffer with data |
37 |
| -* steps: |
38 |
| - 1. process requests that its buffer be filled by making the `read()` system call |
39 |
| - 1. kernel issuing a command to the disk controller hardware to fetch the data from disk |
40 |
| - 1. disk controller writes the data directly into a kernel memory buffer by DMA (direct memory access) |
41 |
| - 1. kernel copies the data from the temporary buffer to the buffer specified by the process |
42 |
| -* kernel space is where the operating system lives |
43 |
| - * communication with device controllers |
44 |
| - * all I/O flows through kernel space |
45 |
| -* why disk controller not send directly to the buffer in user space? |
46 |
| - * user space (where process lives) is a unprivileged area: code executing there cannot directly access |
47 |
| - hardware devices |
48 |
| - * block-oriented hardware devices such as disk controllers operate on fixed-size data blocks |
49 |
| - * user process may be requesting an oddly sized chunk of data |
50 |
| - * kernel plays the role of intermediary, breaking down and reassembling data as it moves between |
51 |
| - user space and storage devices |
52 |
| -* virtual memory means that artificial, or virtual, addresses are used in place of physical |
53 |
| -(hardware RAM) memory addresses (simulates RAM) |
54 |
| - * more than one virtual address can refer to the same physical memory location |
55 |
| - * virtual memory space can be larger than the actual hardware memory available |
56 |
| - * eliminates copies between kernel and user space by mapping a kernel space address to the same |
57 |
| - physical address as a virtual address in user space, the DMA hardware (which can access only physical |
58 |
| - memory addresses) can fill a buffer that is simultaneously visible to both the kernel and a user space process |
59 |
| - |
60 |
| -## NIO |
61 |
| -# Channels |
62 |
| -* is a conduit to an I/O service (a hardware device, a file or socket) and provides methods for |
63 |
| -interacting with that service |
64 |
| -* socket channel objects are bidirectional |
65 |
| -* allows partial transfer - until buffer's `hasRemaining( )` method returns false |
66 |
| -* cannot be reused - represents a specific connection to a specific I/O service and encapsulates |
67 |
| -the state of that connection |
68 |
| -* when a channel is closed - connection is lost |
69 |
| - |
70 |
| -# Socket Channels |
71 |
| -* models network sockets |
72 |
| -* can operate in nonblocking mode and are selectable |
73 |
| -* it's no longer necessary to dedicate a thread to each socket connection |
74 |
| -* it's possible to perform readiness selection of socket channels using a `Selector` object |
75 |
| -* `SocketChannel`, `ServerSocketChannel` create a peer socket object when they are instantiated |
76 |
| - * classes from `java.net`: `Socket`, `ServerSocket` have been updated to be aware of channels |
77 |
| -* Socket channels delegate protocol operations to the peer socket object |
78 |
| - * `ServerSocketChannel` doesn't have a `bind()` method, we have to fetch the peer |
79 |
| - socket and use it to bind to a port to begin listening for connections |
80 |
| -* readiness selection - channel can be queried to determine if it's ready to perform an operation of interest, |
81 |
| -such as reading or writing |
82 |
| -* sockets are stream-oriented, not packet-oriented |
83 |
| - * bytes sent will arrive in the same order, but |
84 |
| - * sender may write N bytes to a socket, and the receiver gets only K when invoking `read()` |
85 |
| - - remaining part may still be in transit |
86 |
| - |
87 | 8 | # SelectionKey
|
88 | 9 | * a key represents the registration of a particular channel object with a
|
89 | 10 | particular selector object - moreover, we have methods: `channel()`, `selector()`
|
|
0 commit comments