java 高效能網路編程

來源:互聯網
上載者:User
 java.io.*

其是最原始的的最簡的 IO 操作.
通常使用簡單. 適合於並發數量不大的情況.
其 IO 時是阻塞的狀態.
對於多SOCKET 來說可能要每SOCKET建立一個
Thread進行處理. 可想而有1000或更多的情況
下,伺服器會成什麼樣子. 伺服器都在忙於線程交換了.

java.nio.*
這是 Java 的非同步IO 處理包. Socket 一般使用
select 的機制. 就是cpu 不斷的查詢所有的SOCKET
控制代碼,有事件它就報告. 比 java.io.* 有很大的進步
不過使用起來不是特別方便. 但是網上有許多的開源
NIO 架構可以使用. apahce MINA/windy 等. 使用方
法都差不多. 封裝的很好.
但是顯然這種情況CPU的佔用率要高了.

iocp (windows), kqueue(freebsd), 或是 epoll(linux)
高效能的SOCKET.
一般的線上的網遊都使用這些技術進行網路連接的架構.

聽說 QQ Game 就是使用 epoll , 單台 Server 可以支援
萬層級的Socket 串連. 汗了.

讓人高興的是 JDK6.0 開始支援 epoll 了.
A new java.nio.channels.SelectorProvider implementation that is based
on the Linux epoll event notification facility is included.
The epoll facility is available in the Linux 2.6, and newer,
kernels. The new epoll-based SelectorProvider implementation
is more scalable than the traditional poll-based SelectorProvider
implementation when there are thousands of SelectableChannels
registered with a Selector. The new SelectorProvider implementation
 will be used by default when the 2.6 kernel is detected.
The poll-based SelectorProvider will be used when a pre-2.6 kernel is detected.

網上看到 JDK5.0 已經有 epoll的實現了.
The epoll SelectorProvider will be included in 5.0 update 9.
To enable it requires setting the system property
java.nio.channels.spi.SelectorProvider to the value
sun.nio.ch.EPollSelectorProvider

總結:

java.io.*
相當於1000個民兵都去村口等著鬼子,來一個鬼子殺一個.
個人評論:村口地方小站不開呀!

select
相當於 一遍遍的詢問“鬼子進村了嗎?”,“鬼子進村了嗎?”...
個人評論:煩呀,不幹正經事,都磨嘴皮子了.

iocp (windows), kqueue(freebsd), 或是 epoll(linux)
一個人去村口站崗,等鬼子來,鬼子來去報告.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.