原文地址http://www.importnew.com/19816.html
概述
NIO主要有三個(gè)核心部分:Channel(通道),Buffer(緩沖區(qū)),Selector(選擇區(qū))。傳統(tǒng)IO基于字節(jié)流和字符流進(jìn)行操作,而NIO基于Channel和Buffer(緩沖區(qū))進(jìn)行操作,數(shù)據(jù)總是從通道讀取到緩沖區(qū)中,或者從緩沖區(qū)寫入到通道中。Selector(選擇區(qū))用于監(jiān)聽多個(gè)通道的事件(比如:連接打開,數(shù)據(jù)到達(dá))。因此,單個(gè)線程可以監(jiān)聽多個(gè)數(shù)據(jù)通道。
NIO和傳統(tǒng)IO(以下會(huì)簡稱IO)之間第一個(gè)最大的區(qū)別是,IO是面向流的,NIO是面向緩沖區(qū)的。Java IO面向流意味著每次從讀取一個(gè)或多個(gè)字節(jié),直至讀取所有字節(jié),他們沒有被緩存在任何地方。此外,它不能前后移動(dòng)流中的數(shù)據(jù),如果需要前后移動(dòng)流中讀取的數(shù)據(jù),首先需要將它緩存到一個(gè)緩沖區(qū)。NIO的緩沖導(dǎo)向方法略有不同。數(shù)據(jù)讀取到一個(gè)它會(huì)稍后處理的緩沖區(qū),需要時(shí)可在緩沖區(qū)中前后移動(dòng)。這就增加了處理過程中的靈活性。但是,還需要檢查是否該緩沖區(qū)中包含所有您需要處理的數(shù)據(jù)。而且,需確保當(dāng)更多的數(shù)據(jù)讀入緩沖區(qū)時(shí),不要覆蓋緩沖區(qū)中尚未處理的數(shù)據(jù)。
IO的各種流是阻塞的。這意味著,當(dāng)一個(gè)線程調(diào)用read()或write()時(shí),該線程被阻塞,知道有一些數(shù)據(jù)被讀取,或數(shù)據(jù)完全寫入。該線程在此期間不能再干任何事情了。NIO的非阻塞模式,使一個(gè)線程從某通道發(fā)送請(qǐng)求讀取數(shù)據(jù),但是它僅能得到目前可用的數(shù)據(jù),如果目前沒有數(shù)據(jù)可用時(shí),它就什么都不會(huì)獲取,而不是保持線程阻塞,所以直至數(shù)據(jù)變的可以讀取之前,該線程可以繼續(xù)做其他的事情。非阻塞寫也是如此。一個(gè)線程請(qǐng)求寫入一些數(shù)據(jù)到某個(gè)通道,但不需要等待它完全寫入,這個(gè)線程同時(shí)可以去做別的事情。線程通常將非阻塞IO的空閑時(shí)間用于在其他通道上執(zhí)行IO操作,所以一個(gè)單獨(dú)的線程現(xiàn)在可以管理多個(gè)輸入和輸出通道(channel)。
Channel
首先說一下Channel,國內(nèi)大多翻譯成“通道”。Channel和IO中的Stream(流)是差不多一個(gè)等級(jí)的。只不過Stream是單向的,譬如:InputStream,OutputStream。而Chaeel是雙向的,既可以用來進(jìn)行讀操作,又可以用來進(jìn)行寫操作。
NIO的Channel的主要實(shí)現(xiàn)有:
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
這里看名字就可以猜出來:分別可以對(duì)應(yīng)文件IO,UDP和TCP(Server和Client)。后面會(huì)演示一個(gè)案例,基本上是圍繞這四個(gè)類型的Channel進(jìn)行陳述的。
Buffer
NIO中的關(guān)鍵Buffer實(shí)現(xiàn)有:ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer,分別對(duì)應(yīng)基本數(shù)據(jù)類型:byte, char, double, float, int, long, short。當(dāng)然NIO中還有MapperByteBuffer, HeapByteBuffer, DirectByteBuffer等。這里不進(jìn)行陳述。
Selector
Selector運(yùn)行單線程可以處理多個(gè)Channel,如果你的應(yīng)用打開了多個(gè)通道,而且每個(gè)鏈接的流量都很低,使用Selector就會(huì)很方便。例如在一個(gè)聊天服務(wù)器中,客戶端如果要使用Selector,需要向Server的Selector注冊(cè)Channel,然后調(diào)用它的select()方法。這個(gè)方法會(huì)一直阻塞到某個(gè)注冊(cè)的通道有事件就緒。一旦這個(gè)方法返回,線程就可以處理這些事件,事件的例子有如新的連接進(jìn)來,數(shù)據(jù)接收等。
FileChannel
看完上面的例子,對(duì)于第一次接觸到NIO的同學(xué)來說有點(diǎn)云里霧里,只說了一些概念,也沒記住什么,更不要說怎么用了。這里開始通過傳統(tǒng)IO以及更新后的NIO來做對(duì)比,以更形象的突出NIO的用法,進(jìn)而使你對(duì)NIO有一點(diǎn)點(diǎn)了解。
傳統(tǒng)IO vs NIO
首先,案例1是采用FileInputStream讀取文件內(nèi)容的
public static void method2(){
InputStream in = null;
try{
in = new BufferedInputStream(new FileInputStream("src/nomal_io.txt"));
byte [] buf = new byte[1024];
int bytesRead = in.read(buf);
while(bytesRead != -1)
{
for(int i=0;i<bytesRead;i++)
System.out.print((char)buf[i]);
bytesRead = in.read(buf);
}
}catch (IOException e)
{
e.printStackTrace();
}finally{
try{
if(in != null){
in.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
下面這個(gè)案例是對(duì)應(yīng)上面的NIO形式實(shí)現(xiàn)(這里通過RandomAccessFile進(jìn)行操作,也可以通過FileInputStream.getChannel()進(jìn)行操作):
public static void method1(){
RandomAccessFile aFile = null;
try{
aFile = new RandomAccessFile("src/nio.txt","rw");
FileChannel fileChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int bytesRead = fileChannel.read(buf);
System.out.println(bytesRead);
while(bytesRead != -1)
{
buf.flip();
while(buf.hasRemaining())
{
System.out.print((char)buf.get());
}
buf.compact();
bytesRead = fileChannel.read(buf);
}
}catch (IOException e){
e.printStackTrace();
}finally{
try{
if(aFile != null){
aFile.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
通過自習(xí)對(duì)比案例1和案例2,應(yīng)該能看出來,NIO的實(shí)現(xiàn)方式會(huì)更復(fù)雜。有了這個(gè)大概印象后我們進(jìn)入下一步。
Buffer的使用
從案例2中可以總結(jié)出使用Buffer一般遵循下面幾個(gè)步驟:
- 分配空間
ByteBuffer buf = ByteBuffer.allocate(1024); 還有一種allocateDirecotr后面再做講解 - 寫入數(shù)據(jù)到Buffer
int bytesRead = fileChannel.read(buf); - 調(diào)用flip()方法
buf.flip(); - 從Buffer中讀取數(shù)據(jù)
System.out.print((char)buf.get();) - 調(diào)用
clear()方法或者compact()方法
Buffer顧名思義:緩沖區(qū),實(shí)際上是一個(gè)容器,一個(gè)連續(xù)數(shù)組。Channel提供從文件、網(wǎng)絡(luò)讀取數(shù)據(jù)的渠道,但是讀寫的數(shù)據(jù)都必須經(jīng)過Buffer。如下圖

向Buffer中寫數(shù)據(jù)的過程:
- 數(shù)據(jù)通過Channel寫到Buffer
fileChannel.read(buf)) - 通過Buffer的put()方法
buf.put(...)
從Buffer中讀取數(shù)據(jù)的過程:
- 從Buffer讀取到Channel
Channel.write(buf) - 使用get()方法從Buffer中讀取數(shù)據(jù)
buf.get()
可以把Buffer簡單地理解為一組基本數(shù)據(jù)類型的元素列表,它通過幾個(gè)變量來保存這個(gè)數(shù)據(jù)當(dāng)前的位置狀態(tài): capacity, position, limit, mark:
| 名稱 | 說明 |
|---|---|
| capacity | 緩沖區(qū)數(shù)組的總長度 |
| position | 下一個(gè)要操作的數(shù)據(jù)元素的位置 |
| limit | 緩沖區(qū)數(shù)組中不可操作的下一個(gè)元素的位置:limit <= capacity |
| mark | 用于記錄當(dāng)前position的前一個(gè)位置或者默認(rèn)是0 |

如圖:我們通過ByteBuffer.allocate(11)方法創(chuàng)建了一個(gè)11個(gè)byte的數(shù)組的緩沖區(qū),初始狀態(tài)如上圖,position的位置為0,capacity和limit默認(rèn)長度都是數(shù)組長度。當(dāng)我們寫入5個(gè)字符時(shí),變化如下圖:

這時(shí)我們需要將緩沖區(qū)中的5個(gè)字節(jié)數(shù)據(jù)寫入到Channel的通信通道,所以我們調(diào)用ByteBuffer.flip()方法,變化如下圖所示(position設(shè)置為0,并將limit設(shè)置成之前的position位置)

這時(shí)底層操作系統(tǒng)就可以從緩沖區(qū)中正確讀取這5個(gè)字節(jié)數(shù)據(jù)并發(fā)送出去了。在下一次寫數(shù)據(jù)之前我們?cè)僬{(diào)用
clear()方法,緩沖區(qū)的索引位置又回到了初始位置。
clear()方法:position將被設(shè)置回0,limit設(shè)置成capacity,換句話說,Buffer被清空了,其實(shí)Buffer中的數(shù)據(jù)并未被清空,只是這些標(biāo)記告訴我們可以從哪里開始往Buffer里寫數(shù)據(jù)。如果Buffer中有一些未讀的數(shù)據(jù),調(diào)用clear()方法,數(shù)據(jù)將“被遺忘”,意味著不再有標(biāo)記會(huì)告訴你哪些數(shù)據(jù)被讀過,哪些還沒有。如果Buffer中仍有未讀的數(shù)據(jù),且后續(xù)還需要這些數(shù)據(jù),但是此時(shí)想要先去寫這些數(shù)據(jù),那么使用compact()方法。
compact()方法:將所有未讀取的數(shù)據(jù)拷貝到Buffer起始處。然后將position設(shè)置到最后一個(gè)未讀元素的后面。limit屬性依然像clear()方法一樣,設(shè)置成capacity?,F(xiàn)在Buffer準(zhǔn)備好寫數(shù)據(jù)了,但是不會(huì)覆蓋未讀的數(shù)據(jù)。
通過調(diào)用Buffer.mark()方法,可以標(biāo)記Buffer的一個(gè)特定的position,之后可以通過調(diào)用Buffer.reset()方法恢復(fù)到這個(gè)position。Buffer.rewind()方法將position設(shè)回0,所以你可以重讀Buffer中的所有數(shù)據(jù)。limit保持不變,仍然表示能從Buffer中讀取多少個(gè)元素。
SocketChannel
說完了FileChannel和Buffer,應(yīng)該對(duì)Buffer的用法比較了解了,這里使用SocketChannel來繼續(xù)探討NIO。NIO的強(qiáng)大功能部分來自于Channel的非阻塞特性,套接字的某些操作可能會(huì)無限期的阻塞。例如,對(duì)accept()方法的調(diào)用可能會(huì)因?yàn)榈却粋€(gè)客戶端連接而阻塞;對(duì)read()方法的調(diào)用可能會(huì)因?yàn)闆]有數(shù)據(jù)可讀而阻塞,知道鏈接的另一端來新的數(shù)據(jù)??偟膩碚f,創(chuàng)建/接收連接或讀寫數(shù)據(jù)等I/O調(diào)用,都可能無限期地阻塞等待,直到底層的網(wǎng)絡(luò)實(shí)現(xiàn)發(fā)生了什么。慢速的,有損耗的網(wǎng)絡(luò),或僅僅是簡單的網(wǎng)絡(luò)故障都可能導(dǎo)致任意時(shí)間的延遲。然而不行的是,在調(diào)用一個(gè)方法之前無法知道其是否會(huì)阻塞。NIO的channel湊想的一個(gè)重要特征就是可以通過配置它的阻塞行為,以實(shí)現(xiàn)非阻塞式的信道。
channel.configureBlocking(false)
在非阻塞式信道上調(diào)用一個(gè)方法總是會(huì)立即返回。這種調(diào)用的返回值指示了所有的操作完成的程度。例如,在一個(gè)非阻塞式ServerSocketChannel上調(diào)用accept()方法,如果有連接請(qǐng)求來了,則返回客戶端SocketChannel,否則返回null。
這里先舉一個(gè)TCP應(yīng)用案例,客戶端采用NIO實(shí)現(xiàn),而服務(wù)端依舊使用IO實(shí)現(xiàn)。
客戶端代碼(NIO)
public static void client(){
ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketChannel socketChannel = null;
try
{
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("10.10.195.115",8080));
if(socketChannel.finishConnect())
{
int i=0;
while(true)
{
TimeUnit.SECONDS.sleep(1);
String info = "I'm "+i+++"-th information from client";
buffer.clear();
buffer.put(info.getBytes());
buffer.flip();
while(buffer.hasRemaining()){
System.out.println(buffer);
socketChannel.write(buffer);
}
}
}
}
catch (IOException | InterruptedException e)
{
e.printStackTrace();
}
finally{
try{
if(socketChannel!=null){
socketChannel.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
服務(wù)端代碼(IO)
public static void server(){
ServerSocket serverSocket = null;
InputStream in = null;
try
{
serverSocket = new ServerSocket(8080);
int recvMsgSize = 0;
byte[] recvBuf = new byte[1024];
while(true){
Socket clntSocket = serverSocket.accept();
SocketAddress clientAddress = clntSocket.getRemoteSocketAddress();
System.out.println("Handling client at "+clientAddress);
in = clntSocket.getInputStream();
while((recvMsgSize=in.read(recvBuf))!=-1){
byte[] temp = new byte[recvMsgSize];
System.arraycopy(recvBuf, 0, temp, 0, recvMsgSize);
System.out.println(new String(temp));
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally{
try{
if(serverSocket!=null){
serverSocket.close();
}
if(in!=null){
in.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
根據(jù)案例分析,總結(jié)一下SocketChannel的用法
打開SocketChannel:
socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("10.10.195.115",8080));
關(guān)閉:
serverSocket.close();
讀取數(shù)據(jù):
String info = "I'm "+i+++"-th information from client";
buffer.clear();
buffer.put(info.getBytes());
buffer.flip();
while(buffer.hasRemaining()){
System.out.println(buffer);
socketChannel.write(buffer);
}
注意SocketChannel.write()方法的調(diào)用是在一個(gè)while循環(huán)中的。write()方法無法保證能寫多少字節(jié)到SocketChannel。所以,我們重復(fù)調(diào)用write()直到Buffer沒有要寫的字節(jié)為止。
非阻塞模式下,read ()方法在尚未讀取到任何數(shù)據(jù)時(shí)可能就返回了。所以需要關(guān)注它的int返回值,它會(huì)告訴你讀取了多少字節(jié)。
TCP服務(wù)端的NIO寫法
到目前為止,所有的案例中都沒有涉及Selector。Selector類可以用于避免使用阻塞式客戶端中很浪費(fèi)資源的“忙等”方法。例如,考慮一個(gè)IM服務(wù)器。像QQ或者旺旺這樣的,可能有幾萬甚至幾千萬個(gè)客戶端同時(shí)連接到了服務(wù)器,但在任何時(shí)刻都只是非常少量的消息。
需要讀取和分發(fā),這就需要一種方法阻塞等待,直到至少有一個(gè)信道可以進(jìn)行I/O操作,并且指出是哪個(gè)信道。NIO的選擇器就是先了這樣的功能。一個(gè)Selector實(shí)例可以同時(shí)檢查一組信道的I/O狀態(tài)。用專業(yè)術(shù)語來說,選擇器就是一個(gè)多路開關(guān)選擇器,因?yàn)橐粋€(gè)選擇器能夠管理多個(gè)信道上的I/O操作。然而如果用傳統(tǒng)方式來處理這么多客戶端,使用的方法是循環(huán)地一個(gè)個(gè)地去檢查所有客戶端是否有I/O操作,如果當(dāng)前客戶端有I/O操作,則可能把當(dāng)前客戶端扔給一個(gè)線程池去處理,如果沒有I/O操作則進(jìn)行下一個(gè)輪詢,當(dāng)所有客戶端都輪詢過了又接著從頭開始輪詢;這種方法是非常笨重而且也非常浪費(fèi)資源,因?yàn)榇蟛糠挚蛻舳耸菦]有I/O操作的,但是我們也必須要去檢查;而selector就不一樣,它在內(nèi)部可以同時(shí)管理多個(gè)I/O,當(dāng)一個(gè)信道有I/O操作的時(shí)候,他會(huì)通知selector,selector就是記住這個(gè)信道有I/O操作,并且知道是何種I/O操作,是讀呢?是寫呢?還是接受新的連接;所以如果使用selector,它返回的結(jié)果只有兩種結(jié)果,一種是0,即在你調(diào)用的時(shí)刻沒有任何客戶端需要I/O操作,另一種結(jié)果是一組需要I/O操作的客戶端,所以使用selector不再需要循環(huán)做輪詢檢查,返回的結(jié)果就是需要的有I/O操作的客戶端。
要使用selector,需要?jiǎng)?chuàng)建一個(gè)selector實(shí)例(使用靜態(tài)工廠方法open())并將其注冊(cè)register到想要監(jiān)控的信道上(注意,這要通過channel的方法實(shí)現(xiàn),而不是使用selector的方法)。最后,調(diào)用選擇器的select()方法。該方法會(huì)阻塞等待,直到有一個(gè)或更多的信道準(zhǔn)備好了I/O操作或等待超時(shí)。select()方法將返回可進(jìn)行I/O操作的信道數(shù)量。現(xiàn)在,在一個(gè)單獨(dú)的線程中,通過調(diào)用select()方法就能檢查多個(gè)信道是否準(zhǔn)備好進(jìn)行I/O操作。如果經(jīng)過一段時(shí)間后仍然沒有信道準(zhǔn)備好,select()方法就會(huì)返回0,并允許程序繼續(xù)執(zhí)行其他任務(wù)。
下面將上面的TCP服務(wù)端代碼改寫成NIO的方式:
public class ServerConnect
{
private static final int BUF_SIZE=1024;
private static final int PORT = 8080;
private static final int TIMEOUT = 3000;
public static void main(String[] args)
{
selector();
}
public static void handleAccept(SelectionKey key) throws IOException{
ServerSocketChannel ssChannel = (ServerSocketChannel)key.channel();
SocketChannel sc = ssChannel.accept();
sc.configureBlocking(false);
sc.register(key.selector(), SelectionKey.OP_READ,ByteBuffer.allocateDirect(BUF_SIZE));
}
public static void handleRead(SelectionKey key) throws IOException{
SocketChannel sc = (SocketChannel)key.channel();
ByteBuffer buf = (ByteBuffer)key.attachment();
long bytesRead = sc.read(buf);
while(bytesRead>0){
buf.flip();
while(buf.hasRemaining()){
System.out.print((char)buf.get());
}
System.out.println();
buf.clear();
bytesRead = sc.read(buf);
}
if(bytesRead == -1){
sc.close();
}
}
public static void handleWrite(SelectionKey key) throws IOException{
ByteBuffer buf = (ByteBuffer)key.attachment();
buf.flip();
SocketChannel sc = (SocketChannel) key.channel();
while(buf.hasRemaining()){
sc.write(buf);
}
buf.compact();
}
public static void selector() {
Selector selector = null;
ServerSocketChannel ssc = null;
try{
selector = Selector.open();
ssc= ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(PORT));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
while(true){
if(selector.select(TIMEOUT) == 0){
System.out.println("==");
continue;
}
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
while(iter.hasNext()){
SelectionKey key = iter.next();
if(key.isAcceptable()){
handleAccept(key);
}
if(key.isReadable()){
handleRead(key);
}
if(key.isWritable() && key.isValid()){
handleWrite(key);
}
if(key.isConnectable()){
System.out.println("isConnectable = true");
}
iter.remove();
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(selector!=null){
selector.close();
}
if(ssc!=null){
ssc.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
下面來講解這段代碼
ServerSocketChannel
打開ServerSocketChannel:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
關(guān)閉ServerSocketChannel:
serverSocketChannel.close();
監(jiān)聽新進(jìn)來的連接:
while(true){
SocketChannel socketChannel = serverSocketChannel.accept();
}
ServerSocketChannel可以設(shè)置成非阻塞模式。在非阻塞模式下,accept()方法會(huì)立刻返回,如果還沒有新進(jìn)來的連接,返回的將是null。因此,需要檢查返回的SocketChannel是否是null,如:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(9999));
serverSocketChannel.configureBlocking(false);
while (true)
{
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null)
{
// do something with socketChannel...
}
}
Selector
Selector的創(chuàng)建:
Selector selector = Selector.open()
為了將Channel和Selector配合使用,必須將Channel注冊(cè)到Selector上,通過SelectableChannel.register方法來實(shí)現(xiàn),沿用案例5中的部分代碼:
ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(PORT));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
與Selector一起使用時(shí),Channel必須處于非阻塞模式下。這意味著不能將FileChannel與Selector一起使用,因?yàn)镕ileChannel不能切換到非阻塞模式。而SocketChannel兩種模式都可以。
注意register()方法的第二個(gè)參數(shù)。這是一個(gè)"interest集合",意思是在通過Selector監(jiān)聽Channel時(shí)對(duì)什么事件感興趣??梢约菜姆N不同類型的事件:
1. Connect
2. Accept
3. Read
4. Write
通道觸發(fā)了一個(gè)事件的意思是該事件已經(jīng)就緒。所以,某個(gè)channel成功連接到另一個(gè)服務(wù)器成為連接就緒。一個(gè)ServerSocketChanel準(zhǔn)備好接收新進(jìn)入的鏈接成為接收就緒。一個(gè)有數(shù)據(jù)可讀的通道可以說是讀取就緒。等待寫數(shù)據(jù)的通道可以說是寫就緒
這四種事件用SelectionKey的四個(gè)常量來表示:
1. SelectionKey.OP_CONNECT
2. SelectionKey.OP_ACCEPT
3. SelectionKey.OP_READ
4. SelectionKey.OP_WRITE
SelectionKey
當(dāng)向Selector注冊(cè)Channel時(shí),register()方法會(huì)返回一個(gè)SelectionKey對(duì)象。這個(gè)對(duì)象如下屬性:
- interest集合
- ready集合
- Channel
- Selector
- 附加的對(duì)象(可選)
interest集合:就像向Selector注冊(cè)通道的部分所說的,interest集合是你所選擇的感興趣的事件集合??梢酝ㄟ^SelectionKey讀寫interest集合。
ready集合:是通道已經(jīng)準(zhǔn)備就緒的操作的集合。在一次選擇(Selection)之后,你回首先訪問這個(gè)ready set。Selection將會(huì)在下一個(gè)小姐進(jìn)行解釋??梢赃@樣訪問ready集合:
int readySet = selectionKey.readyOps();
可以用像檢測(cè)interest集合那樣的方法,來檢測(cè)channel中什么事件或操作已經(jīng)就緒。但是,也可以使用以下四個(gè)方法,它們都會(huì)返回一個(gè)布爾類型:
selectionKey.isAcceptable();
selectionKey.isConnectable();
selectionKey.isReadable();
selectionKey.isWritable();
從SelectionKey訪問Chennel和Selector的API也比較簡單,如下:
Channel channel = selectionKey.channel();
Selector selector = selectionKey.selector();
可以將一個(gè)對(duì)象或者更多信息附著到SelectionKey上,這樣就能方便的識(shí)別某個(gè)給定的通道。例如,可以附加與通道一起使用的Buffer,或是包含聚集數(shù)據(jù)的某個(gè)對(duì)象。使用方法如下:
selectionKey.attch(theObject)
Object attachedObj = selectionKey.attchment();
還可以用register()方法向Selector注冊(cè)Channel的時(shí)候附加對(duì)象。如:
SelectionKey key = channel.register(selector, SelectionKey.OP_READ, theObject);
通過Selector選擇通道
一旦向Selector注冊(cè)了一個(gè)或多個(gè)通道,就可以調(diào)用幾個(gè)重載的select()方法。這些方法返回你所感興趣的事件(如連接、接受、讀或?qū)懀┮呀?jīng)準(zhǔn)備就緒的那些通道。換句話說,如果你對(duì)“讀就緒”的通道感興趣,select()方法會(huì)返回讀事件已經(jīng)就緒的那些通道。
下面是select()的方法:
int select()
int select(long timeout)
int selectNow()
select()阻塞到至少有一個(gè)通道在你注冊(cè)的事件上就緒了。
select(long timeout)和select()一樣,除了會(huì)最長阻塞timeout毫秒后超時(shí)。
selectNow()不會(huì)阻塞,不管什么通道就緒了就會(huì)立刻返回(此方法執(zhí)行非阻塞的選擇操作。如果自從前一次選擇操作后,沒有通道變成可選擇的,則此方法直接返回0)。
select()方法返回的int值表示有多少通道已經(jīng)就緒。解釋下就是,自從上次調(diào)用了select()方法之后,有多少個(gè)通道變成了就緒狀態(tài)。如果調(diào)用select()方法,因?yàn)闀?huì)有一個(gè)通道變成就緒狀態(tài),返回了1,若再次調(diào)用select()方法,如果另一個(gè)通道就緒了,它會(huì)再次返回1.如果對(duì)第一個(gè)就緒的channel沒有做任何操作,現(xiàn)在就有兩個(gè)就緒的通道,但在每次select()方法調(diào)用之前,只有一個(gè)通道就緒了。
一旦調(diào)用了select()方法,并且返回值表名有一個(gè)或更多個(gè)通道就緒了,然后可以通過調(diào)用selector的selectedKeys()方法,訪問“已選擇鍵集(selected key set)”中的就緒通道。如下所示:
Set selectedKeys = selector.selectedKeys();
當(dāng)向Selector注冊(cè)Channel時(shí),Channel.register()方法會(huì)返回一個(gè)SelectionKey對(duì)象。這個(gè)對(duì)象代表了注冊(cè)到該Selector的通道??梢酝ㄟ^SelectionKey的selectedKeySet()方法訪問這些對(duì)象。
注意,每次迭代末尾的keyIterator.remove()的調(diào)用。Selector不會(huì)自己從已選擇鍵集中移除SelectionKey實(shí)例。必須在處理完通道時(shí)自己移除。下次該通道變成就緒時(shí),Selector會(huì)再次將其放入已選擇的鍵集中。
SelectionKey.channel()方法返回的通道需要轉(zhuǎn)型成你要處理的類型,如ServerSocketChannel或SocketChannel等。
一個(gè)完整的使用Selector和ServerSocketChannel的案例可以參考案例5中的selector()方法。
內(nèi)存映射文件
Java處理大文件,一般用BufferedReader,BufferedInputStream這類緩沖的IO類,不過如果文件超大的話,更快的方式是采用MappedByteBuffer。
MappedByteBuffer是NIO引入的文件內(nèi)存映射方案,讀寫性能極高。NIO最主要的就是實(shí)現(xiàn)了對(duì)異步操作的支持。其中一種通過把一個(gè)套接字通道(SocketChannel)注冊(cè)到一個(gè)選擇器(Selector)中,不時(shí)的調(diào)用后者的select()方法就能返回滿足的SelectionKey,這個(gè)SelectionKey中包含了SOCKET事件信息。這就是select模型。
SocketChannel的讀寫是通過一個(gè)類叫ByteBuffer來操作的。這個(gè)類本身的設(shè)計(jì)是不錯(cuò)的,比直接操作byte[]方便很多。ByteBuffer有兩種模式:直接/間接。間接模式是最經(jīng)典的(也只有這么一種)就是HeapByteBuffer,即操作堆內(nèi)存(byte[])。但是內(nèi)存畢竟有限,如果我要發(fā)送一個(gè)1G的文件怎么辦?不可能真的去分配1G的內(nèi)存。這是就必須使用“直接”模式,即MappedByteBuffer文件映射。
先中斷一下,談?wù)劜僮飨到y(tǒng)的內(nèi)存管理。一般操作系統(tǒng)的內(nèi)存分為兩個(gè)部分:物理內(nèi)存和虛擬內(nèi)存。虛擬內(nèi)存一般使用的時(shí)頁面映像文件,即磁盤中的某個(gè)(某些)特殊的文件。操作系統(tǒng)負(fù)責(zé)頁面文件內(nèi)容的讀寫,這個(gè)過程叫做“頁面中斷/切換”。MappedByteBuffer也是類似的,你可以把整個(gè)文件(不管文件有多大)看成是一個(gè)ByteBuffer。MappedByteBuffer只是一種特殊的ByteBuffer,即ByteBuffer的子類。MappedByteBuffer將文件直接映射到內(nèi)存(這里的內(nèi)存指的是虛擬內(nèi)存,并不是物理內(nèi)存)。通常,可以映射整個(gè)文件,如果文件比較大的話可以分段進(jìn)行映射,只要指定文件的那個(gè)部分就可以。
概念
FileChanel提供了map方法來把文件映射為內(nèi)存映像文件:MappedByteBuffer map(int mode, long position, long size);可以把文件從position開始的size大小的區(qū)域映射為內(nèi)存映像文件,mode指出了可訪問該內(nèi)存映像文件的方式:
- READ_ONLY:試圖修改得到的緩沖區(qū)將導(dǎo)致拋出ReadOnlyBufferException。(MapMode.READ_ONLY)。
- READ_WRITE(讀/寫): 對(duì)得到的緩沖區(qū)的更改最終將傳到文件;該更改對(duì)映射到同一文件的其他程序不一定是可見的。 (MapMode.READ_WRITE)
- PRIVATE(專用): 對(duì)得到的緩沖區(qū)的更改不會(huì)傳到文件,并且該更改對(duì)映射到同一文件的其他程序也不是可見的;相反,會(huì)創(chuàng)建緩沖區(qū)已修改部分的專用副本。 (MapMode.PRIVATE)
MappedByteBuffer是ByteBuffer的子類,其擴(kuò)充了三個(gè)方法:
- force():緩沖區(qū)是READ_WRITE模式下,此方法對(duì)緩沖區(qū)內(nèi)容的修改強(qiáng)行寫入文件;
- load():將緩沖區(qū)的內(nèi)容載入內(nèi)存,并返回該緩沖區(qū)的引用;
- isLoaded():如果緩沖區(qū)的內(nèi)容在物理內(nèi)存中,則返回真,否則返回假;
案例對(duì)比
這里通過采用ByteBuffer和MappedByteBuffer分別讀取大小約為5M的文件“src/1.ppt”來比較兩者之間的區(qū)別,method3()是采用MappedByteBuffer讀取的,method4()對(duì)應(yīng)的是ByteBuffer。
public static void method4(){
RandomAccessFile aFile = null;
FileChannel fc = null;
try{
aFile = new RandomAccessFile("src/1.ppt","rw");
fc = aFile.getChannel();
long timeBegin = System.currentTimeMillis();
ByteBuffer buff = ByteBuffer.allocate((int) aFile.length());
buff.clear();
fc.read(buff);
//System.out.println((char)buff.get((int)(aFile.length()/2-1)));
//System.out.println((char)buff.get((int)(aFile.length()/2)));
//System.out.println((char)buff.get((int)(aFile.length()/2)+1));
long timeEnd = System.currentTimeMillis();
System.out.println("Read time: "+(timeEnd-timeBegin)+"ms");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(aFile!=null){
aFile.close();
}
if(fc!=null){
fc.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void method3(){
RandomAccessFile aFile = null;
FileChannel fc = null;
try{
aFile = new RandomAccessFile("src/1.ppt","rw");
fc = aFile.getChannel();
long timeBegin = System.currentTimeMillis();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, aFile.length());
// System.out.println((char)mbb.get((int)(aFile.length()/2-1)));
// System.out.println((char)mbb.get((int)(aFile.length()/2)));
//System.out.println((char)mbb.get((int)(aFile.length()/2)+1));
long timeEnd = System.currentTimeMillis();
System.out.println("Read time: "+(timeEnd-timeBegin)+"ms");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(aFile!=null){
aFile.close();
}
if(fc!=null){
fc.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
通過在入口函數(shù)main()中運(yùn)行:
method3();
System.out.println("=============");
method4();
輸出結(jié)果(運(yùn)行在普通PC機(jī)上):
Read time: 2ms
=============
Read time: 12ms
通過輸出結(jié)果可以看出彼此的差別,一個(gè)例子也許是偶然,那么下面把5M大小的文件替換為200M的文件,輸出結(jié)果:
Read time: 1ms
=============
Read time: 407ms
可以看到差距拉大。
注:MappedByteBuffer有資源釋放的問題:被MappedByteBuffer打開的文件只有在垃圾收集時(shí)才會(huì)被關(guān)閉,而這個(gè)點(diǎn)是不確定的。在Javadoc中這里描述:A mapped byte buffer and the file mapping that it represents remian valid until the buffer itself is garbage-collected。詳細(xì)可以翻閱參考資料5和6.
其余功能介紹
看完上面的陳述,相信大家對(duì)NIO有了一定的了解,下面主要通過幾個(gè)案例,來說明NIO的其余功能,下面代碼量偏多,功能性講述偏少。
Scatter/Gatter
分散(scatter)從Channel中讀取是指在讀操作時(shí)將讀取的數(shù)據(jù)寫入多個(gè)buffer中。因此,Channel將從Channel中讀取的數(shù)據(jù)“分散(scatter)”到多個(gè)Buffer中
聚集(gather)寫入Channel是指在寫操作時(shí)將多個(gè)buffer的數(shù)據(jù)寫入同一個(gè)Channel,因此,Channel將多個(gè)Buffer中的數(shù)據(jù)“聚集(gather)”后發(fā)送到Channel。
案例:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
public class ScattingAndGather
{
public static void main(String args[]){
gather();
}
public static void gather()
{
ByteBuffer header = ByteBuffer.allocate(10);
ByteBuffer body = ByteBuffer.allocate(10);
byte [] b1 = {'0', '1'};
byte [] b2 = {'2', '3'};
header.put(b1);
body.put(b2);
ByteBuffer [] buffs = {header, body};
try
{
FileOutputStream os = new FileOutputStream("src/scattingAndGather.txt");
FileChannel channel = os.getChannel();
channel.write(buffs);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
transferFrom & transferTo
FileChannel的transferFrom()方法可以將數(shù)據(jù)從源通道傳輸?shù)紽ileChannel中。
public static void method1(){
RandomAccessFile fromFile = null;
RandomAccessFile toFile = null;
try
{
fromFile = new RandomAccessFile("src/fromFile.xml","rw");
FileChannel fromChannel = fromFile.getChannel();
toFile = new RandomAccessFile("src/toFile.txt","rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
System.out.println(count);
toChannel.transferFrom(fromChannel, position, count);
}
catch (IOException e)
{
e.printStackTrace();
}
finally{
try{
if(fromFile != null){
fromFile.close();
}
if(toFile != null){
toFile.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
方法的輸入?yún)?shù)position表示從position處開始向目標(biāo)文件寫入數(shù)據(jù),count表示最多傳輸?shù)淖止?jié)數(shù)。如果源通道的剩余空間小于 count 個(gè)字節(jié),則所傳輸?shù)淖止?jié)數(shù)要小于請(qǐng)求的字節(jié)數(shù)。此外要注意,在SoketChannel的實(shí)現(xiàn)中,SocketChannel只會(huì)傳輸此刻準(zhǔn)備好的數(shù)據(jù)(可能不足count字節(jié))。因此,SocketChannel可能不會(huì)將請(qǐng)求的所有數(shù)據(jù)(count個(gè)字節(jié))全部傳輸?shù)紽ileChannel中。
transferTo()方法將數(shù)據(jù)從FileChannel傳輸?shù)狡渌腸hannel中。
public static void method2()
{
RandomAccessFile fromFile = null;
RandomAccessFile toFile = null;
try
{
fromFile = new RandomAccessFile("src/fromFile.txt","rw");
FileChannel fromChannel = fromFile.getChannel();
toFile = new RandomAccessFile("src/toFile.txt","rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
System.out.println(count);
fromChannel.transferTo(position, count,toChannel);
}
catch (IOException e)
{
e.printStackTrace();
}
finally{
try{
if(fromFile != null){
fromFile.close();
}
if(toFile != null){
toFile.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
上面所說的關(guān)于SocketChannel的問題在transferTo()方法中同樣存在。SocketChannel會(huì)一直傳輸數(shù)據(jù)直到目標(biāo)buffer被填滿。
Pipe
Java NIO管道是2個(gè)線程之間的單向數(shù)據(jù)連接。Pipe有一個(gè)source通道和一個(gè)sink通道。數(shù)據(jù)會(huì)被寫到sink通道,從source通道讀取。
public static void method1(){
Pipe pipe = null;
ExecutorService exec = Executors.newFixedThreadPool(2);
try{
pipe = Pipe.open();
final Pipe pipeTemp = pipe;
exec.submit(new Callable<Object>(){
@Override
public Object call() throws Exception
{
Pipe.SinkChannel sinkChannel = pipeTemp.sink();//向通道中寫數(shù)據(jù)
while(true){
TimeUnit.SECONDS.sleep(1);
String newData = "Pipe Test At Time "+System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()){
System.out.println(buf);
sinkChannel.write(buf);
}
}
}
});
exec.submit(new Callable<Object>(){
@Override
public Object call() throws Exception
{
Pipe.SourceChannel sourceChannel = pipeTemp.source();//向通道中讀數(shù)據(jù)
while(true){
TimeUnit.SECONDS.sleep(1);
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
int bytesRead = sourceChannel.read(buf);
System.out.println("bytesRead="+bytesRead);
while(bytesRead >0 ){
buf.flip();
byte b[] = new byte[bytesRead];
int i=0;
while(buf.hasRemaining()){
b[i]=buf.get();
System.out.printf("%X",b[i]);
i++;
}
String s = new String(b);
System.out.println("=================||"+s);
bytesRead = sourceChannel.read(buf);
}
}
}
});
}catch(IOException e){
e.printStackTrace();
}finally{
exec.shutdown();
}
}
DatagramChannel
Java NIO中的DatagramChannel是一個(gè)能收發(fā)UDP包的通道。因?yàn)閁DP是無連接的網(wǎng)絡(luò)協(xié)議,所以不能像其他通道那樣讀取和寫入。它發(fā)送和接收的是數(shù)據(jù)包。
public static void reveive(){
DatagramChannel channel = null;
try{
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(8888));
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
channel.receive(buf);
buf.flip();
while(buf.hasRemaining()){
System.out.print((char)buf.get());
}
System.out.println();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(channel!=null){
channel.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void send(){
DatagramChannel channel = null;
try{
channel = DatagramChannel.open();
String info = "I'm the Sender!";
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
buf.put(info.getBytes());
buf.flip();
int bytesSent = channel.send(buf, new InetSocketAddress("10.10.195.115",8888));
System.out.println(bytesSent);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(channel!=null){
channel.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
可以通過閱讀參考資料2和3了解更多的NIO細(xì)節(jié)知識(shí),前人栽樹后人乘涼,這里就不贅述啦。
參考資料
- JAVA NIO http://tutorials.jenkov.com/java-nio/nio-vs-io.html
- JAVA NIO 系列教程 http://ifeve.com/java-nio-all/
- JAVA NIO詳解 http://www.cnblogs.com/phoebus0501/archive/2010/12/05/1897245.html
- 《深入分析Java Web技術(shù)內(nèi)幕》許令波 著
- java大文件讀寫操作,java nio 之MappedByteBuffer,高效文件/內(nèi)存映射 http://langgufu.iteye.com/blog/2107023
- java nio 之MappedByteBuffer,高效文件/內(nèi)存映射 http://www.360doc.com/content/13/0302/18/11314689_268892455.shtml#