http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf
Web services, Distributed Objects, etc, Most have same basic structure:
Read request
Decode request
Process service
Encode reply
Send reply
Scalability Goals
" Graceful degradation under increasing load (more clients)
" Continuous improvement with increasing resources (CPU, memory, disk, bandwidth)
" Also meet availability and performance goals
Short latencies
Meeting peak demand
Tunable quality of service
" Divide-and-conquer is usually the best approach for achieving any scalability goal
http://www.cnblogs.com/luxiaoxun/p/4331110.html
Basic Reactor Design
Divide and Conquer
" Divide processing into small tasks
Each task performs an action without blocking
" Execute each task when it is enabled
Here, an IO event usually serves as trigger
Basic mechanisms supported in java.nio
Non-blocking reads and writes
Dispatch tasks associated with sensed IO events
" Endless variation possible
A family of event-driven designs
Event-driven Designs
Background: Events in AWT
AWT Event Queue
Reactor Pattern
" Reactor responds to IO events by dispatching
the appropriate handler
Similar to AWT thread
" Handlers perform non-blocking actions
Similar to AWT ActionListeners
" Manage by binding handlers to events
Similar to AWT addActionListener
java.nio Support
" Channels
Connections to files, sockets etc that support
non-blocking reads
" Buffers
Array-like objects that can be directly read or
written by Channels
" Selectors
Tell which of a set of Channels have IO events
" SelectionKeys
Maintain IO event status and bindings
Web services, Distributed Objects, etc, Most have same basic structure:
Read request
Decode request
Process service
Encode reply
Send reply
Scalability Goals
" Graceful degradation under increasing load (more clients)
" Continuous improvement with increasing resources (CPU, memory, disk, bandwidth)
" Also meet availability and performance goals
Short latencies
Meeting peak demand
Tunable quality of service
" Divide-and-conquer is usually the best approach for achieving any scalability goal
http://www.cnblogs.com/luxiaoxun/p/4331110.html
class Server implements Runnable { public void run() { try { ServerSocket ss = new ServerSocket(PORT); while (!Thread.interrupted()) new Thread(new Handler(ss.accept())).start(); //创建新线程来handle // 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) { /* ... */ } } }
对于每一个请求都分发给一个线程,每个线程中都独自处理上面的流程。
这种模型由于IO在阻塞时会一直等待,因此在用户负载增加时,性能下降的非常快。
server导致阻塞的原因:
1、serversocket的accept方法,阻塞等待client连接,直到client连接成功。
2、线程从socket inputstream读入数据,会进入阻塞状态,直到全部数据读完。
3、线程向socket outputstream写入数据,会阻塞直到全部数据写完。
client导致阻塞的原因:
1、client建立连接时会阻塞,直到连接成功。
2、线程从socket输入流读入数据,如果没有足够数据读完会进入阻塞状态,直到有数据或者读到输入流末尾。
3、线程从socket输出流写入数据,直到输出所有数据。
4、socket.setsolinger()设置socket的延迟时间,当socket关闭时,会进入阻塞状态,直到全部数据都发送完或者超时。
改进:采用基于事件驱动的设计,当有事件触发时,才会调用处理器进行数据处理。
Basic Reactor Design
Divide and Conquer
" Divide processing into small tasks
Each task performs an action without blocking
" Execute each task when it is enabled
Here, an IO event usually serves as trigger
Basic mechanisms supported in java.nio
Non-blocking reads and writes
Dispatch tasks associated with sensed IO events
" Endless variation possible
A family of event-driven designs
Event-driven Designs
Background: Events in AWT
AWT Event Queue
Reactor Pattern
" Reactor responds to IO events by dispatching
the appropriate handler
Similar to AWT thread
" Handlers perform non-blocking actions
Similar to AWT ActionListeners
" Manage by binding handlers to events
Similar to AWT addActionListener
" Channels
Connections to files, sockets etc that support
non-blocking reads
" Buffers
Array-like objects that can be directly read or
written by Channels
" Selectors
Tell which of a set of Channels have IO events
" SelectionKeys
Maintain IO event status and bindings