初窥Java网络IO

1,040 views

Published on

A brief introduction to the Java I/O (NIO, AIO).

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,040
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

初窥Java网络IO

  1. 1. 初窥 Java 网络 IO DigitalSonic 2011-10
  2. 2. 同步?异步?阻塞?非阻塞?UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking
  3. 3. IO in Java● JDK 1.4 —— NIO ● 我们有了 Non-Blocking IO● JDK 1.7—— NIO2 ● 我们有了 Asynchronous IO ● Linux 下通过 epoll 模拟实现 ● Windows 下通过 IOCP 实现
  4. 4. 传统 Socket 服务器实现方式● 用 ServerSocket 绑定某个端口● 循环 accept ● 开启一个新线程处理 Socket ● 处理完毕后关闭 Socket
  5. 5. NIO – How To● 非阻塞 ServerSocketChannel 绑定端口● 注册选择器● 循环 select ● 处理 select 的结果 key ● 处理完毕移除 key
  6. 6. NIO 范例
  7. 7. NIO 范例
  8. 8. AIO – How To● 配置线程池● AsynchronousServerSocketChannel 绑定端口● accept ● 返回 Future 做进一步处理 ● 设定 CompletionHandler ,在其中做进一步处理
  9. 9. AIO 范例
  10. 10. AIO 范例
  11. 11. AIO 范例
  12. 12. AIO 范例Start listening on 8888.sun.nio.ch.UnixAsynchronousServerSocketChannelImpl[/0:0:0:0:0:0:0:0:8888]AsynchronousSocketChannel Accepted!sun.nio.ch.UnixAsynchronousSocketChannelImpl[connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716]sun.nio.ch.UnixAsynchronousSocketChannelImpl[connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] said: abcsun.nio.ch.UnixAsynchronousSocketChannelImpl[connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] said: exit

×