java nio doug_Scalable IO in Java -Doug Lea

【实例简介】

Scalable IO in Java -Doug Lea

描述java nio 和reactor 设计模式之间的关系

Network services

Web services Distributed objects. etc

Most have same basic structure

Read request

Decode request

Process service

Encode repl

ly

Send reply

But differ in nature and cost of each step

XML parsing, File transfer, Web page

generation, computational services

Classic Service Designs

client

ead(decode compute(encode( send

handler

ead)(decode compute (encode( send

client

Server

handler

client

read )(decode compute(encode( send

ha

andler

Each handler may be started in its own thread

Classic Server Socket Loop

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 latencⅰes

Meeting peak demand

Tunable quality of service

Divide-and-conquer is usually the best

approach for achieving any scalability goal

Divide and Conquer

Divide processing into small tasks

Each task performs an action without blocking

Execute each task when it is enabled

Here, an lo event usually serves as

handler

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

Usually more efficient than alternatives

Fewer resources

Don't usually need a thread per client

ess overhead

Less context switching, often less locking

But dispatching can be slower

Must manually bind actions to events

Usually harder to program

Must break up into simple non-blocking actions

Similar to gul event-driven actions

Cannot eliminate all blocking: GC, page faults, etc

Must keep track of logical state of service

Background: Events in AWT

AWT Event Queue

Event

Button

Event

aWT thread

click!

Actionlistener

Event-driven lO uses similar ideas but in different designs

Reactor Pattern

Reactor responds to lO 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

See schmidt et al. pattern-Oriented software

Architecture, Volume 2(POSA2)

Also Richard stevens s networking books matt

Welsh's seda framework. etc

【实例截图】

【核心代码】

你可能感兴趣的:(java,nio,doug)