407 lines
13 KiB
Markdown
407 lines
13 KiB
Markdown
|
<!-- GFM-TOC -->
|
|||
|
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
|
|||
|
* [<EFBFBD><EFBFBD><EFBFBD>̲<EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD>̲<EFBFBD><CCB2><EFBFBD>)
|
|||
|
* [<EFBFBD>ֽڲ<EFBFBD><EFBFBD><EFBFBD>](#<23>ֽڲ<D6BD><DAB2><EFBFBD>)
|
|||
|
* [<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [1. InetAddress](#1-inetaddress)
|
|||
|
* [2. URL](#2-url)
|
|||
|
* [3. Sockets](#3-sockets)
|
|||
|
* [4. Datagram](#4-datagram)
|
|||
|
* [NIO](#nio)
|
|||
|
* [1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#1-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [2. ͨ<><CDA8><EFBFBD>뻺<EFBFBD><EBBBBA><EFBFBD><EFBFBD>](#2-ͨ<><CDA8><EFBFBD>뻺<EFBFBD><EBBBBA><EFBFBD><EFBFBD>)
|
|||
|
* [2.1 ͨ<><CDA8>](#21-ͨ<><CDA8>)
|
|||
|
* [2.2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#22-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>](#3-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>)
|
|||
|
* [4. <20><>д<EFBFBD>ļ<EFBFBD>ʵ<EFBFBD><CAB5>](#4-<2D><>д<EFBFBD>ļ<EFBFBD>ʵ<EFBFBD><CAB5>)
|
|||
|
* [5. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#5-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [5.1 <20><><EFBFBD><EFBFBD>ʽ I/O](#51-<2D><><EFBFBD><EFBFBD>ʽ-io)
|
|||
|
* [5.2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ I/O](#52-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ-io)
|
|||
|
* [6. <20><EFBFBD><D7BD><EFBFBD>ʵ<EFBFBD><CAB5>](#6-<2D><EFBFBD><D7BD><EFBFBD>ʵ<EFBFBD><CAB5>)
|
|||
|
* [6.1 ServerSocketChannel](#61-serversocketchannel)
|
|||
|
* [6.2 Selectors](#62-selectors)
|
|||
|
* [6.3 <20><>ѭ<EFBFBD><D1AD>](#63-<2D><>ѭ<EFBFBD><D1AD>)
|
|||
|
* [6.4 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#64-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [6.5 <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>](#65-<2D><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [6.6 ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SelectionKey](#66-ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-selectionkey)
|
|||
|
* [6.7 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> I/O](#67-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-io)
|
|||
|
* [<EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
<!-- GFM-TOC -->
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
Java <20><> I/O <20><><EFBFBD>ſ<EFBFBD><C5BF>Էֳ<D4B7><D6B3><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
|
|||
|
|
|||
|
1. <20><><EFBFBD>̲<EFBFBD><CCB2><EFBFBD><EFBFBD><EFBFBD>File
|
|||
|
2. <20>ֽڲ<D6BD><DAB2><EFBFBD><EFBFBD><EFBFBD>InputStream <20><> OutputStream
|
|||
|
3. <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Reader <20><> Writer
|
|||
|
4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Serializable
|
|||
|
5. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Socket
|
|||
|
6. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ IO<49><4F>NIO
|
|||
|
|
|||
|
# <20><><EFBFBD>̲<EFBFBD><CCB2><EFBFBD>
|
|||
|
|
|||
|
File <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD>ʾ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ڱ<EFBFBD>ʾ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
|||
|
# <20>ֽڲ<D6BD><DAB2><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8143787f-12eb-46ea-9bc3-c66d22d35285.jpg)
|
|||
|
|
|||
|
Java I/O ʹ<><CAB9><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>ʵ<EFBFBD>֡<EFBFBD><D6A1><EFBFBD> InputStream Ϊ<><CEAA><EFBFBD><EFBFBD>InputStream <20>dz<EFBFBD><C7B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FileInputStream <20><> InputStream <20><><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><E0A3AC><EFBFBD>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṩ<EFBFBD><E1B9A9><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FilterInputStream <20><><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD>װ<EFBFBD><D7B0><EFBFBD>ߣ<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ṩ<EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD> BufferedInputStream Ϊ FileInputStream <20>ṩ<EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9>ܡ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB>湦<EFBFBD>ܵ<EFBFBD><DCB5>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ֻ<EFBFBD><D6BB>Ҫ<EFBFBD><D2AA> FileInputStream <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> BufferedInputStream <20><><EFBFBD>ɡ<F3BCB4BF>
|
|||
|
|
|||
|
```java
|
|||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|||
|
```
|
|||
|
|
|||
|
DataInputStream װ<><D7B0><EFBFBD><EFBFBD><EFBFBD>ṩ<EFBFBD>˶Ը<CBB6><D4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> int<6E><74>double <20>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
byte[] buf = new byte[20*1024];
|
|||
|
int bytes = 0;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ buf.length <20><><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD>ʵ<EFBFBD>ʶ<EFBFBD>ȡ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1 <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD> eof<6F><66><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>β
|
|||
|
while((bytes = in.read(buf, 0 , buf.length)) != -1) {
|
|||
|
// ...
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD><EFBFBD>̻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>紫<EFBFBD>䣬<EFBFBD><EFBFBD>С<EFBFBD>Ĵ洢<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBF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ƕ<EFBFBD><C7B6>̳<EFBFBD><CCB3><EFBFBD> Reader <20><> Writer<65><72>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>ַ<EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD>ֽڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
byte[] bytes = str.getBytes(encoding); // <20><><EFBFBD><EFBFBD>
|
|||
|
String str = new String(bytes, encoding)<29><> // <20><><EFBFBD><EFBFBD>
|
|||
|
```
|
|||
|
|
|||
|
GBK <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>ռ 2 <20><><EFBFBD>ֽڣ<D6BD>Ӣ<EFBFBD><D3A2>ռ 1 <20><><EFBFBD>ֽڣ<D6BD>UTF-8 <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>ռ 3 <20><><EFBFBD>ֽڣ<D6BD>Ӣ<EFBFBD><D3A2>ռ 1 <20><><EFBFBD>ֽڣ<D6BD>Java ʹ<><CAB9>˫<EFBFBD>ֽڱ<D6BD><DAB1><EFBFBD> UTF-16be<62><65><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD>Ӣ<EFBFBD>Ķ<EFBFBD>ռ 2 <20><><EFBFBD>ֽڡ<D6BD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ò<EFBFBD>ͬ<EFBFBD>ı<EFBFBD><EFBFBD>뷽ʽ<EFBFBD><EFBFBD>ô<EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>롣
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD>ʹ<EFBFBD><EFBFBD>䡣
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>ObjectOutputStream.writeObject()
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>ObjectInputStream.readObject()
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʵ<EFBFBD><EFBFBD> Serializable <20>ӿڣ<D3BF><DAA3><EFBFBD>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><D7BC>û<EFBFBD><C3BB><EFBFBD>κη<CEBA><CEB7><EFBFBD><EFBFBD><EFBFBD>Ҫʵ<D2AA>֡<EFBFBD>
|
|||
|
|
|||
|
transient <20>ؼ<EFBFBD><D8BC>ֿ<EFBFBD><D6BF><EFBFBD>ʹһЩ<D2BB><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BFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
|||
|
```
|
|||
|
private transient Object[] elementData;
|
|||
|
```
|
|||
|
|
|||
|
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
Java <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD>֣<EFBFBD>
|
|||
|
|
|||
|
1. InetAddress<73><73><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD> IP <20><>ַ<EFBFBD><D6B7>
|
|||
|
2. URL<52><4C>ͳһ<CDB3><D2BB>Դ<EFBFBD><D4B4>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8> URL <20><><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӷ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
|||
|
3. Sockets<74><73>ʹ<EFBFBD><CAB9> TCP Э<><D0AD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>ţ<EFBFBD>
|
|||
|
4. Datagram<61><6D>ʹ<EFBFBD><CAB9> UDP Э<><D0AD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>š<EFBFBD>
|
|||
|
|
|||
|
## 1. InetAddress
|
|||
|
|
|||
|
û<EFBFBD>й<EFBFBD><EFBFBD>й<EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> InetAddress.getByName(String host)<29><>InetAddress.getByAddress(byte[] addr)<29><>
|
|||
|
|
|||
|
## 2. URL
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӵ<EFBFBD> URL <20>ж<EFBFBD>ȡ<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
URL url = new URL("http://www.baidu.com");
|
|||
|
InputStream is = url.openStream(); // <20>ֽ<EFBFBD><D6BD><EFBFBD>
|
|||
|
InputStreamReader isr = new InputStreamReader(is, "utf-8"); // <20>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
BufferedReader br = new BufferedReader(isr);
|
|||
|
String line = br.readLine();
|
|||
|
while (line != null) {
|
|||
|
System.out.println(line);
|
|||
|
line = br.readLine();
|
|||
|
}
|
|||
|
br.close();
|
|||
|
isr.close();
|
|||
|
is.close();
|
|||
|
```
|
|||
|
|
|||
|
## 3. Sockets
|
|||
|
|
|||
|
Socket ͨ<><CDA8>ģ<EFBFBD><C4A3>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/fa4101d7-19ce-4a69-a84f-20bbe64320e5.jpg)
|
|||
|
|
|||
|
- ServerSocket<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
- Socket<65><74><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳͻ<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD> InputStream <20><> OutputStream <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
## 4. Datagram
|
|||
|
|
|||
|
- DatagramPacket<65><74><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD>
|
|||
|
- DatagramSocket<65><74>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
# NIO
|
|||
|
|
|||
|
NIO <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> I/O <20><><EFBFBD><EFBFBD> ( <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ) ת<>ƻز<C6BB><D8B2><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>Աȥ<D4B1><C8A5><EFBFBD>ƾͿ<C6BE><CDBF>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȡ<D9B6>
|
|||
|
|
|||
|
## 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
I/O <20><> NIO <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4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<DDB1><C8B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>öࡣ<C3B6><E0A1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> I/O ȱ<><C8B1>һЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> I/O <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժͼ<D4BA><CDBC><EFBFBD><EFBFBD>ԡ<EFBFBD>
|
|||
|
|
|||
|
I/O <20><><EFBFBD><EFBFBD> NIO <20>Ѿ<EFBFBD><D1BE>ܺõؼ<C3B5><D8BC><EFBFBD><EFBFBD>ˣ<EFBFBD>java.io.\* <20>Ѿ<EFBFBD><D1BE><EFBFBD> NIO Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NIO <20><>һЩ<D2BB><D0A9><EFBFBD>ԡ<EFBFBD><D4A1><EFBFBD><EFBFBD>磬 java.io.\* <20><><EFBFBD>е<EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>д<EFBFBD><D0B4><EFBFBD>ݵķ<DDB5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ü<EFBFBD>ʹ<EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>졣
|
|||
|
|
|||
|
## 2. ͨ<><CDA8><EFBFBD>뻺<EFBFBD><EBBBBA><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
### 2.1 ͨ<><CDA8>
|
|||
|
|
|||
|
ͨ<EFBFBD><EFBFBD> Channel <20>Ƕ<EFBFBD>ԭ I/O <20><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
|||
|
ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD>֮ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD>(һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> InputStream <20><><EFBFBD><EFBFBD> OutputStream <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<29><> <20><>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD>ڶ<EFBFBD>д<EFBFBD><D0B4>
|
|||
|
|
|||
|
ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
|||
|
|
|||
|
- FileChannel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>ж<EFBFBD>д<EFBFBD><D0B4><EFBFBD>ݣ<EFBFBD>
|
|||
|
- DatagramChannel<65><6C>ͨ<EFBFBD><CDA8> UDP <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
|||
|
- SocketChannel<65><6C>ͨ<EFBFBD><CDA8> TCP <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
|||
|
- ServerSocketChannel<65><6C><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD> TCP <20><><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD>ÿһ<C3BF><D2BB><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6>ᴴ<EFBFBD><E1B4B4>һ<EFBFBD><D2BB> SocketChannel<65><6C>
|
|||
|
|
|||
|
### 2.2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFB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BFBD><EFBFBD><EFBFBD>ṩ<EFBFBD>˶<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĽṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><EFBFBD><EFBFBD>ϵͳ<EFBFBD>Ķ<EFBFBD>/д<><D0B4><EFBFBD>̡<EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
|||
|
|
|||
|
- ByteBuffer
|
|||
|
- CharBuffer
|
|||
|
- ShortBuffer
|
|||
|
- IntBuffer
|
|||
|
- LongBuffer
|
|||
|
- FloatBuffer
|
|||
|
- DoubleBuffer
|
|||
|
|
|||
|
|
|||
|
## 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
- capacity<74><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
- position<6F><6E><EFBFBD><EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
- limit<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>д<EFBFBD><D0B4><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸı<EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD>
|
|||
|
|
|||
|
1\. <20>½<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>СΪ 8 <20><><EFBFBD>ֽڵĻ<DAB5><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ position Ϊ 0<><30><EFBFBD><EFBFBD> limit == capacity == 9<><39>capacity <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı䣬<C4B1><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۻ<EFBFBD><DBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1bea398f-17a7-4f67-a90b-9e2d243eaa9a.png)
|
|||
|
|
|||
|
2\. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ж<EFBFBD>ȡ 3 <20><><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>뻺<EFBFBD><EBBBBA><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ʱ position <20>ƶ<EFBFBD><C6B6><EFBFBD>Ϊ 3<><33>limit <20><><EFBFBD>ֲ<EFBFBD><D6B2>䡣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4628274c-25b6-4053-97cf-d1239b44c43d.png)
|
|||
|
|
|||
|
3\. <20>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>֮ǰ<D6AE><C7B0><EFBFBD><EFBFBD>Ҫ<EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD> flip() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> limit <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ǰ position<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD> position <20><><EFBFBD><EFBFBD>Ϊ 0<><30>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/952e06bd-5a65-4cab-82e4-dd1536462f38.png)
|
|||
|
|
|||
|
4\. <20>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ 4 <20><><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ʱ position <20><>Ϊ 4<><34>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b5bdcbe2-b958-4aef-9151-6ad963cb28b4.png)
|
|||
|
|
|||
|
5\. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> clear() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ position <20><> limit <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/67bf5487-c45d-49b6-b9c0-a058d8c68902.png)
|
|||
|
|
|||
|
## 4. <20><>д<EFBFBD>ļ<EFBFBD>ʵ<EFBFBD><CAB5>
|
|||
|
|
|||
|
1\. ΪҪ<CEAA><D2AA>ȡ<EFBFBD><C8A1><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD> FileInputStream<61><6D>֮<EFBFBD><D6AE>ͨ<EFBFBD><CDA8> FileInputStream <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> FileChannel<65><6C>
|
|||
|
|
|||
|
```java
|
|||
|
FileInputStream fin = new FileInputStream("readandshow.txt");
|
|||
|
FileChannel fic = fin.getChannel();
|
|||
|
```
|
|||
|
|
|||
|
2\. <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1024 <20><> Buffer
|
|||
|
|
|||
|
```java
|
|||
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
|||
|
```
|
|||
|
|
|||
|
3\. <20><><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD> FileChannel д<>뵽 Buffer <20>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ݵĻ<DDB5><C4BB><EFBFBD> read() <20><><EFBFBD><EFBFBD><EFBFBD>᷵<EFBFBD><E1B7B5> -1
|
|||
|
|
|||
|
```java
|
|||
|
int r = fcin.read(buffer);
|
|||
|
if (r == -1) {
|
|||
|
break;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
4\. ΪҪд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD> FileOutputStream<61><6D>֮<EFBFBD><D6AE>ͨ<EFBFBD><CDA8> FileOutputStream <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> FileChannel
|
|||
|
|
|||
|
```java
|
|||
|
FileOutputStream fout = new FileOutputStream("writesomebytes.txt");
|
|||
|
FileChannel foc = fout.getChannel();
|
|||
|
```
|
|||
|
|
|||
|
5\. <20><><EFBFBD><EFBFBD> flip() <20>л<EFBFBD><D0BB><EFBFBD>д
|
|||
|
|
|||
|
```java
|
|||
|
buffer.flip();
|
|||
|
```
|
|||
|
|
|||
|
6\. <20><> Buffer <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> FileChannel <20><>
|
|||
|
|
|||
|
```java
|
|||
|
foc.write(buffer);
|
|||
|
```
|
|||
|
|
|||
|
7\. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> clear() <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
buffer.clear();
|
|||
|
```
|
|||
|
|
|||
|
## 5. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
Ӧ<EFBFBD><EFBFBD>ע<EFBFBD>⣬FileChannel <20><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><D7BD><EFBFBD> Channel <20><><EFBFBD>ԡ<EFBFBD>
|
|||
|
|
|||
|
### 5.1 <20><><EFBFBD><EFBFBD>ʽ I/O
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ I/O <20>ڵ<EFBFBD><DAB5><EFBFBD> InputStream.read() <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>һֱ<D2BB>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ż᷵<C5BB>أ<EFBFBD><D8A3>ڵ<EFBFBD><DAB5><EFBFBD> ServerSocket.accept() <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ҳ<EFBFBD><D2B2>һֱ<D2BB><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>пͻ<D0BF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӲŻ᷵<C5BB>أ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD><EFBFBD><EFBFBD><F3A3ACB7><EFBFBD><EFBFBD>˶<EFBFBD><CBB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>߳<EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD>ÿͻ<C3BF><CDBB>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/edc23f99-c46c-4200-b64e-07516828720d.jpg)
|
|||
|
|
|||
|
### 5.2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ I/O
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ר<EFBFBD>ŵ<EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> I/O <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD>߳<EFBFBD>ͨ<EFBFBD>ţ<EFBFBD><EFBFBD>߳<EFBFBD>֮<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD> wait()<29><>notify() <20>ȷ<EFBFBD>ʽͨ<CABD>ţ<EFBFBD><C5A3><EFBFBD>֤ÿ<D6A4><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ν<EFBFBD><CEBD><EFBFBD>߳<EFBFBD><DFB3>л<EFBFBD><D0BB><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7fcb2fb0-2cd9-4396-bc2d-282becf963c3.jpg)
|
|||
|
|
|||
|
## 6. <20><EFBFBD><D7BD><EFBFBD>ʵ<EFBFBD><CAB5>
|
|||
|
|
|||
|
### 6.1 ServerSocketChannel
|
|||
|
|
|||
|
ÿһ<EFBFBD><EFBFBD><EFBFBD>˿ڶ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> ServerSocketChannel <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
ServerSocketChannel ssc = ServerSocketChannel.open();
|
|||
|
ssc.configureBlocking(false); // <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
ServerSocket ss = ssc.socket();
|
|||
|
InetSocketAddress address = new InetSocketAddress(ports[i]);
|
|||
|
ss.bind(address); // <20>˿ں<CBBF>
|
|||
|
```
|
|||
|
|
|||
|
### 6.2 Selectors
|
|||
|
|
|||
|
<EFBFBD>첽 I/O ͨ<><CDA8> Selector ע<><D7A2><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD> I/O <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>Ȥ <20>D <20>ɶ<EFBFBD><C9B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĵ<DDB5><C4B5><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><D7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵȵȣ<C8B5><C8A3>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>ʱ<EFBFBD><CAB1>ϵͳ<CFB5><CDB3><EFBFBD>ᷢ<EFBFBD><E1B7A2>֪ͨ<CDA8><D6AA>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> Selectors ֮<>Ϳ<F3A3ACBE><CDBF>ԶԲ<D4B6>ͬ<EFBFBD><CDAC>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> register() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>register() <20>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Selector<6F><72><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> OP_ACCEPT<50><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> accept <20>¼<EFBFBD><C2BC><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
|
|||
|
|
|||
|
SelectionKey <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ڴ<EFBFBD> Selector <20>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ᡣ<EFBFBD><E1A1A3>ij<EFBFBD><C4B3> Selector ֪ͨ<CDA8><D6AA>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ṩ<EFBFBD><E1B9A9>Ӧ<EFBFBD>ڸ<EFBFBD><DAB8>¼<EFBFBD><C2BC><EFBFBD> SelectionKey <20><><EFBFBD><EFBFBD><EFBFBD>еġ<D0B5>SelectionKey <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ע<EFBFBD>ᡣ
|
|||
|
|
|||
|
```java
|
|||
|
Selector selector = Selector.open();
|
|||
|
SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
|
|||
|
```
|
|||
|
|
|||
|
### 6.3 <20><>ѭ<EFBFBD><D1AD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><EFBFBD><EFBFBD> Selector <20><> select() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>߸<EFBFBD><DFB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> select() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><EFBFBD><EFBFBD> Selector <20><> selectedKeys() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD> SelectionKey <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> <20><><EFBFBD><EFBFBD> <20><>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SelectionKeys <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ÿ<EFBFBD><C3BF> SelectionKey <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB> SelectionKey<65><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʲô I/O <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD>Щ I/O <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
int num = selector.select();
|
|||
|
|
|||
|
Set selectedKeys = selector.selectedKeys();
|
|||
|
Iterator it = selectedKeys.iterator();
|
|||
|
|
|||
|
while (it.hasNext()) {
|
|||
|
SelectionKey key = (SelectionKey)it.next();
|
|||
|
// ... deal with I/O event ...
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
### 6.4 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ServerSocketChannel<65><6C><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>ǡ<EFBFBD><C7A1><EFBFBD><EFBFBD>ա<EFBFBD><D5A1>¼<EFBFBD><C2BC><EFBFBD>Ϊȷ<CEAA><C8B7><EFBFBD><EFBFBD>һ<EFBFBD>㣬<EFBFBD><E3A3AC><EFBFBD>Ƕ<EFBFBD> SelectionKey <20><><EFBFBD><EFBFBD> readyOps() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>鷢<EFBFBD><E9B7A2><EFBFBD><EFBFBD>ʲô<CAB2><C3B4><EFBFBD>͵<EFBFBD><CDB5>¼<EFBFBD><C2BC><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
if ((key.readyOps() & SelectionKey.OP_ACCEPT)
|
|||
|
== SelectionKey.OP_ACCEPT) {
|
|||
|
// Accept the new connection
|
|||
|
// ...
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>Կ϶<EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD> readOps() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><C7B8>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|||
|
|
|||
|
### 6.5 <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD> accept() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
|
|||
|
SocketChannel sc = ssc.accept();
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD> SocketChannel <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ġ<EFBFBD><C4A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>Ϊ<EFBFBD>˶<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD>뽫 SocketChannel ע<>ᵽ Selector<6F>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
|
|||
|
|
|||
|
```java
|
|||
|
sc.configureBlocking( false );
|
|||
|
SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ );
|
|||
|
```
|
|||
|
|
|||
|
ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> register() <20><> OP_READ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SocketChannel ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>ȡ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|||
|
|
|||
|
### 6.6 ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SelectionKey
|
|||
|
|
|||
|
<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD> SelectionKey ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǽ<EFBFBD><C7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>ˡ<EFBFBD><CBA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SelectionKey <20><>ѡ<EFBFBD><D1A1><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD>ᵼ<EFBFBD><E1B5BC><EFBFBD><EFBFBD><EFBFBD>dz<EFBFBD><C7B3><EFBFBD><EFBFBD>ٴδ<D9B4><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> remove() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SelectionKey<65><79>
|
|||
|
|
|||
|
```java
|
|||
|
it.remove();
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> I/O <20>¼<EFBFBD>)<29>ˡ<EFBFBD>
|
|||
|
|
|||
|
### 6.7 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> I/O
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ᴥ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> I/O <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ᵼ<EFBFBD><E1B5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>е<EFBFBD><D0B5><EFBFBD> Selector.select()<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD> I/O <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>Σ<EFBFBD> SelectionKey <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ OP_READ <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
|
|||
|
|
|||
|
```java
|
|||
|
} else if ((key.readyOps() & SelectionKey.OP_READ)
|
|||
|
== SelectionKey.OP_READ) {
|
|||
|
// Read the data
|
|||
|
SocketChannel sc = (SocketChannel)key.channel();
|
|||
|
// ...
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
# <20>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
- Eckel B, <20><><EFBFBD>˶<EFBFBD> , <20><><EFBFBD><EFBFBD> , <20><> . Java <20><><EFBFBD><EFBFBD>˼<EFBFBD><CBBC> [M]. <20><>е<EFBFBD><D0B5>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> , 2002.
|
|||
|
- [IBM: NIO <20><><EFBFBD><EFBFBD>](https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html)
|
|||
|
- [ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Java I/O <20>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ](https://www.ibm.com/developerworks/cn/java/j-lo-javaio/index.html)
|
|||
|
- [NIO <20>봫ͳ IO <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ](http://blog.csdn.net/shimiso/article/details/24990499)
|