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

【实例截图】

【核心代码】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值