没有合适的资源?快使用搜索试试~ 我知道了~
非阻塞式网络服务器 nio.pdf recator
需积分: 10 30 下载量 83 浏览量
2009-04-30
15:35:21
上传
评论
收藏 158KB PDF 举报
温馨提示
试读
39页
如何从菜鸟变高手,青蛙变王子,灰姑娘变公主?赶紧下载,非同一般。 有下载好的,请推荐同类书籍。南无阿弥陀佛!
资源推荐
资源详情
资源评论
http://gee.cs.oswego.edu
Scalable IO in Java
Doug Lea
State University of New York at Oswego
dl@cs.oswego.edu
http://gee.cs.oswego.edu
http://gee.cs.oswego.edu
Outline
" Scalable network services
" Event-driven processing
" Reactor pattern
Basic version
Multithreaded versions
Other variants
" Walkthrough of java.nio nonblocking IO APIs
http://gee.cs.oswego.edu
Network Services
" Web services, Distributed Objects, etc
" Most have same basic structure:
Read request
Decode request
Process service
Encode reply
Send reply
" But differ in nature and cost of each step
XML parsing, File transfer, Web page
generation, computational services, ...
http://gee.cs.oswego.edu
Classic Service Designs
client
client
client
Server
read
decode
compute
encode
send
read
decode
compute
encode
send
handler
handler
read
decode
compute
encode
send
handler
Each handler may be started in its own thread
http://gee.cs.oswego.edu
Classic ServerSocket Loop
class Server implements Runnable {
public void run() {
try {
ServerSocket ss = new ServerSocket(PORT);
while (!Thread.interrupted())
new Thread(new Handler(ss.accept())).start();
// or, single-threaded, or a thread pool
} catch (IOException ex) { /* ... */ }
}
static class Handler implements Runnable {
final Socket socket;
Handler(Socket s) { socket = s; }
public void run() {
try {
byte[] input = new byte[MAX_INPUT];
socket.getInputStream().read(input);
byte[] output = process(input);
socket.getOutputStream().write(output);
} catch (IOException ex) { /* ... */ }
}
private byte[] process(byte[] cmd) { /* ... */ }
}
}
Note: most exception handling elided from code examples
剩余38页未读,继续阅读
资源评论
jimieweile
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功