Java NIO(六)選取器

來源:互聯網
上載者:User

標籤:selected   post   ==   selector   上下文   open   之間   建立   nio   

Selector選取器是Java NIO中能夠檢測一到多個NIO通道,並能夠知曉通道是否為諸如讀寫事件做好準備的組件。這樣使得一個單獨的線程可以管理多個Channel,從而管理多個網路連接。

為什麼使用Selector

對於作業系統來說,線程之間環境切換的開銷很大,而且每個線程都要佔用系統的資源,所以使用Selector單獨管理多個Channel

Selector的建立
Selector selector = Selector.open();

 

向Selector註冊通道
channel.configureBlocking(false);SelectionKey selectionKey = channel.register(selector, SelectionKey.OP_READ);

 

與Selector一起使用時,通道必須處於非阻塞模式,這意味著FileChannel不能與Selector一起使用。通訊端通道都可以

register()方法的第二個參數是一個“interest集合”,意思是通過Selector監聽Channel時對什麼事件感興趣。可以監聽四種不同類型的事件,用四種常量表示:

  • SelectionKey.OP_CONNECT: 串連就緒(某個Channel成功串連到另一個伺服器)
  • SelectionKey.OP_ACCEPT: 接收就緒(一個ServerSocketChannel準備好接收新進入的串連)
  • SelectionKey.OP_READ: 讀就緒(一個有資料可讀的通道)
  • SelectionKey.OP_WRITE: 寫就緒(等待寫資料的通道)

通道觸發了一個事件,就表示該事件已經就緒。如果對不止一種事件感興趣,可以用位或操作符將常量串連:

int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;

 

SelectionKey

這個對象包含了一些屬性:

  • interest集合
  • ready集合
  • Channel
  • Selector
  • 附加的對象(可選)
interest集合

interest集合是你所選擇的感興趣的事件集合。可以通過SelectionKey讀寫interest集合

int interestSet = selectionKey.interestOps();boolean isAccept = (interest & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;
boolean isConnect = (interest & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT;
boolean isRead = (interest & SelectionKey.OP_READ) == SelectionKey.OP_READ;
boolean isWrite = (interest & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE;

 

用位與操作interest集合和SelectionKey常量可以判斷某個確定的事件是否在interest集合中

ready集合

ready集合是通道已經準備就緒的操作的集合。在一次選擇(Selection)之後,會首先訪問ready set。

int readySet = selectionKey.readyOps();

 

可以像檢測interest集合一樣,檢測Channel中有哪些事件或操作已經準備就緒,也可以用以下方法:

boolean isAccept = selectionKey.isAcceptable();boolean isConnect = selectionKey.isConnectable();
boolean isRead = selectionKey.isReadable();
boolean isWrite = selectionKey.isWriteable();

 

Channel和Selector
Channel channel = selectionKey.channel();Selector selector = selectionKey.selector();

 

附加對象

可以將一個對象或更多資訊附加到SelectionKey上,這樣能方便識別某個通道。例如,可以附加與通道一起使用的Buffer,或是包含聚集資料的某個對象

selectionKey.attach(object);Object obj = selectionKey.attachment();

 

還可以用register()方法向Selector註冊Channel時附加對象

SelectionKey selectionKey = channel.register(selector, SelectionKey.OP_READ, object);

 

通過Selector選擇通道

一旦向Selector註冊了一個或多個通道,就可以調用幾個重載的select()方法。這些方法會返回你所感興趣的事件已經準備就緒的通道。

  • int select()
  • int select(long timeout)
  • int selectNow()

select()方法會阻塞到至少有一個通道在你註冊的事件上就緒了

select(long timeout)和select()一樣,除了最長會阻塞timeout毫秒

selectNow()不會阻塞,不管什麼通道就緒都立刻返回

select()方法返回的int值表示有多少通道已經就緒,即自從上次調用select()方法後有多少通道變成了就緒狀態。如果調用一次select()方法,因為有一個通道變成了就緒狀態,返回1,如果再次調用select()方法,如果另一個通道也就緒了,則還會返回1。即使,第一個就緒的通道沒有做任何操作,但是每次select()方法調用之間,只有一個通道變成就緒狀態

selectedKeys()

一旦調用了select()方法,且傳回值表明有一個或多個通道就緒了,然後可以調用Selector的selectedKeys()方法,訪問“已選擇鍵集“(selected key set)中的就緒通道

Set selectedKeys = selector.selectedKeys();

 

當向Selector註冊Channel時,Channel.register()方法會返回一個SelectionKey對象。這個對象代表了註冊到該Selector的通道。可以通過Selector的selectedKeys()方法訪問這些對象

wakeUp()

某個線程調用select()方法之後阻塞了,即使沒有通道已經就緒,也可以讓其返回。只要讓其他線程在第一個線程調用select()方法的對象上調用Selector.wakeUp()方法即可,阻塞在select()方法上的線程會立馬返回

如果有其他線程調用了wakeUp()方法,且線程調用select()方法沒有阻塞,下次調用select()方法的線程會立即醒來

close()

用完Selector後調用close()方法關閉,且是註冊到Selector上的所有SelectionKey執行個體無效,通道本身並不會關閉

 

Java NIO(六)選取器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.